@Resource,@Inject,@Autowire的区别
参考文章:https://www.baeldung.com/spring-annotations-resource-inject-autowire
Two of the three annotations belong to the Java extension package: javax.annotation.Resource and javax.inject.Inject. The @Autowired annotation belongs to the org.springframework.beans.factory.annotation package.
@Resource
The @Resource annotation is part of the JSR-250 annotation collection, and is packaged with J2EE. This annotation has the following execution paths, listed by precedence:
- Match by Name
- Match by Type
- Match by Qualifier
These execution paths are applicable to both setter and field injection.
@Inject
The @Inject annotation belongs to the JSR-330 annotations collection. This annotation has the following execution paths, listed by precedence:
- Match by Type
- Match by Qualifier
- Match by Name
@Autowire
The behaviour of the @Autowired annotation is similar to the @Inject annotation. The only difference is that the @Autowired annotation is part of the Spring framework. This annotation has the same execution paths as the @Inject annotation, listed in order of precedence:
Match by Type
Match by Qualifier
Match by Name
@Component, @Service, @Controller, @Repository, @Configuration
参考文章:https://javarevisited.blogspot.com/2017/11/difference-between-component-service.html#axzz74qAQmb18
Similar to above, in the future Spring may add special functionalities for @Service, @Controller and @Repository based on their layering conventions. Hence, it's always a good idea to respect the convention and use it in line with layers.
@Component
@Component is a generic stereotype for any Spring-managed component,respect to bean creation and dependency injection
@Repository
stereotype for persistence layer
The @Repository
annotation is a marker for any class that fulfils the role or stereotype of a repository (also known as Data Access Object or DAO). Among the uses of this marker is the automatic translation of exceptions, as described in Exception Translation.
@Service
stereotype for service layer,that hold business logic.
@Controller
stereotype for presentation layer (spring-mvc)。This latter property is used by web-specific tools and functionalities.For example, DispatcherServlet will look for @RequestMapping on classes that are annotated using @Controller but not with @Component.
@Configuration
-
@Configuration
is meta-annotated with@Component
, therefore@Configuration
classes are candidates for component scanning - Properties resolved through the
Environment
reside in one or more "property source" objects, and@Configuration
classes may contribute property sources to theEnvironment
object using the@PropertySource
annotation:
@Configuration
@PropertySource("classpath:/com/acme/app.properties")
public class AppConfig {
@Inject Environment env;
@Bean
public MyBean myBean() {
return new MyBean(env.getProperty("bean.name"));
}
}
- Externalized values may be injected into
@Configuration
classes using the@Value
annotation:
@Configuration
@PropertySource("classpath:/com/acme/app.properties")
public class AppConfig {
@Value("${bean.name}") String beanName;
@Bean
public MyBean myBean() {
return new MyBean(beanName);
}
}
-
@Configuration
classes may be marked with the@Profile
annotation to indicate they should be processed only if a given profile or profiles are active:
@Profile("development")
@Configuration
public class EmbeddedDatabaseConfig {
@Bean
public DataSource dataSource() {
// instantiate, configure and return embedded DataSource
}
}