[关闭]
@zhuanxu 2017-11-15T16:56:01.000000Z 字数 2663 阅读 1737

第3章:注释配置

JavaEE开发的颠覆者SpringBoot实战


@Required

第一个注释是 @Required,其用法如下:

  1. public class SimpleMovieLister {
  2. private MovieFinder movieFinder;
  3. @Required
  4. public void setMovieFinder(MovieFinder movieFinder) {
  5. this.movieFinder = movieFinder;
  6. }
  7. // ...
  8. }

@Required的含义是让容器在初始化的就检查Bean的属性需要被注入,防止在运行时抛出NullPointerException异常。


@Autowired

先看下定义:

  1. @Target({ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface Autowired {
  5. /**
  6. * Declares whether the annotated dependency is required.
  7. * <p>Defaults to {@code true}.
  8. */
  9. boolean required() default true;
  10. }

保留到运行时,然后可以放在基本所有的类型上。运行时IoC容器会根据注释自动注入依赖。


@Primary

此处如果我们使用@Autowired注释,可能带来的问题是会有多个候选者,这个时候我们可以通过@Primary指定到底使用哪个。

  1. public class MovieRecommender {
  2. @Autowired
  3. private MovieCatalog movieCatalog;
  4. // ...
  5. }

xml配置:

  1. <bean class="example.SimpleMovieCatalog" primary="true">
  2. <!-- inject any dependencies required by this bean -->
  3. </bean>
  4. <bean class="example.SimpleMovieCatalog">
  5. <!-- inject any dependencies required by this bean -->
  6. </bean>
  7. <bean id="movieRecommender" class="example.MovieRecommender"/>
  8. </beans>

@Qualifier

当我们需要对@Autowired进行更精细的控制的时候,就可以使用@Qualifier了,相比较于@Primary能够对Bean进行更精细的控制,看例子:

  1. public class MovieRecommender {
  2. private MovieCatalog movieCatalog;
  3. private CustomerPreferenceDao customerPreferenceDao;
  4. @Autowired
  5. public void prepare(@Qualifier("main")MovieCatalog movieCatalog,
  6. CustomerPreferenceDao customerPreferenceDao) {
  7. this.movieCatalog = movieCatalog;
  8. this.customerPreferenceDao = customerPreferenceDao;
  9. }
  10. // ...
  11. }

@Qualifier能够修饰方法和参数,上面的例子是修饰的参数,再来看xml中的配置。

  1. <bean class="example.SimpleMovieCatalog">
  2. <qualifier value="main"/>
  3. <!-- inject any dependencies required by this bean -->
  4. </bean>
  5. <bean class="example.SimpleMovieCatalog">
  6. <qualifier value="action"/>
  7. <!-- inject any dependencies required by this bean -->
  8. </bean>
  9. <bean id="movieRecommender" class="example.MovieRecommender"/>

@Qualifier除了修饰函数和参数外,还能修饰注释本省,看例子:

  1. @Target({ElementType.FIELD, ElementType.PARAMETER})
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Qualifier
  4. public @interface MovieQualifier {
  5. String genre();
  6. Format format();
  7. }

然后代码中的注释:

  1. public class MovieRecommender {
  2. @Autowired
  3. @MovieQualifier(format=Format.VHS, genre="Action")
  4. private MovieCatalog actionVhsCatalog;
  5. @Autowired
  6. @MovieQualifier(format=Format.VHS, genre="Comedy")
  7. private MovieCatalog comedyVhsCatalog;
  8. @Autowired
  9. @MovieQualifier(format=Format.DVD, genre="Action")
  10. private MovieCatalog actionDvdCatalog;
  11. @Autowired
  12. @MovieQualifier(format=Format.BLURAY, genre="Comedy")
  13. private MovieCatalog comedyBluRayCatalog;
  14. // ...
  15. }

@Resource

这个是JSR-250的标准,里面可以声明Bean的名字,如果不声明,则默认取属性名或者方法名,看例子:

  1. public class SimpleMovieLister {
  2. private MovieFinder movieFinder;
  3. //@Resource(name="movieFinder")
  4. @Resource
  5. public void setMovieFinder(MovieFinder movieFinder) {
  6. this.movieFinder = movieFinder;
  7. }
  8. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注