Spring的一些常用注解及其区别

@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:

  1. Match by Name
  2. Match by Type
  3. 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:

  1. Match by Type
  2. Match by Qualifier
  3. 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

参考文章:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html

  1. @Configuration is meta-annotated with @Component, therefore @Configuration classes are candidates for component scanning
  2. Properties resolved through the Environment reside in one or more "property source" objects, and @Configuration classes may contribute property sources to the Environment 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"));
     }
 }
  1. 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);
     }
 }
  1. @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
     }
 }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容