环境:spring5.2.15
@Autowried注解首先根据byType注入,如果有多个的情况会按照byName注入,如果没有符合的名称那么系统将会报错。结合@Qualifier限定注入的Bean。
功能1
- As of Spring Framework 4.3, an @Autowired annotation on such a constructor is no longer necessary if the target bean defines only one constructor to begin with. However, if several constructors are available and there is no primary/default constructor, at least one of the constructors must be annotated with @Autowired in order to instruct the container which one to use.
大意: 从spring4.3开始,如果目标bean只定义了一个构造函数,则不再需要在这样的构造函数上使用@Autowired注释。但是,如果有多个构造函数可用,并且没有主/默认构造函数,则必须至少用@Autowired对其中一个构造函数进行注释,以便指示容器使用哪个构造函数。
示例:
- public class SimpleMovieLister {
- private MovieFinder movieFinder;
- @Autowired
- public void setMovieFinder(MovieFinder movieFinder) {
- this.movieFinder = movieFinder;
- }
- }
功能2
@Autowired可以将注释应用于具有任意名称和多个参数的方法
示例:
- public class MovieRecommender {
- private MovieCatalog movieCatalog;
- private CustomerPreferenceDao customerPreferenceDao;
- @Autowired
- public void prepare(MovieCatalog movieCatalog,CustomerPreferenceDao customerPreferenceDao) {
- this.movieCatalog = movieCatalog;
- this.customerPreferenceDao = customerPreferenceDao;
- }
- }
功能3
可以将@Autowired应用于字段,甚至可以将其与构造函数混合使用
- public class MovieRecommender {
- private final CustomerPreferenceDao customerPreferenceDao;
- @Autowired
- private MovieCatalog movieCatalog;
- @Autowired
- public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
- this.customerPreferenceDao = customerPreferenceDao;
- }
- }
功能4
可以通过将@Autowired注释添加到需要该类型数组的字段或方法上
- public class MovieRecommender {
- @Autowired
- private MovieCatalog[] movieCatalogs;
- }
功能5
可以通过将@Autowired注释添加到需要该类型集合类的字段或方法上
- public class MovieRecommender {
- private Set<MovieCatalog> movieCatalogs;
- @Autowired
- public void setMovieCatalogs(Set<MovieCatalog> movieCatalogs) {
- this.movieCatalogs = movieCatalogs;
- }
- }
注意:如果你希望数组或list列表注入的bean具有顺序,那么你可以对bean实现Ordered接口或者是使用@Order注解或者标准的 @Priority(JavaEE) 注解。上面的Set不具有顺序性
功能6
可以通过将@Autowired注释添加到Map集合的字段或方法上
- public class MovieRecommender {
- private Map<String, MovieCatalog> movieCatalogs;
- @Autowired
- public void setMovieCatalogs(Map<String, MovieCatalog> movieCatalogs) {
- this.movieCatalogs = movieCatalogs;
- }
- }
这里的key为Bean的名称。
注意:对于声明的数组、集合或Map集合,至少需要一个匹配元素,否则会错误。
默认情况下使用@Autowired注解意味着是必须具有相应的bean存在,否则程序报错。可以通过如下方式改变默认行为
- public class SimpleMovieLister {
- private MovieFinder movieFinder;
- @Autowired(required = false)
- public void setMovieFinder(MovieFinder movieFinder) {
- this.movieFinder = movieFinder;
- }
- }
注意:如果这里没有合适的MovieFinder类型的Bean那么这里的setter方法都不会执行。如果这里是通过构造函数注入,那么即便使用了required = false程序还是会报错。
功能7
可以通过Java 8的Java.util.Optional来表示特定依赖项的非必需性质
- public class SimpleMovieLister {
- @Autowired
- public void setMovieFinder(Optional<MovieFinder> movieFinder) {
- }
- }
功能8
使用 @Nullable 注解标准非必须依赖项
- @Autowired
- public void setMovieFinder(@Nullable MovieFinder movieFinder) {
- System.out.println("-----movie") ;
- this.movieFinder = movieFinder;
- }
功能9
@Autowired可以用于注入 BeanFactory, ApplicationContext, Environment, ResourceLoader, ApplicationEventPublisher, and MessageSource
示例:
- @Autowired
- private ApplicationContext context;
注意:
- The @Autowired, @Inject, @Value, and @Resource annotations are handled by Spring BeanPostProcessor implementations. This means that you cannot apply these annotations within your own BeanPostProcessor or BeanFactoryPostProcessor types (if any). These types must be 'wired up' explicitly by using XML or a Spring @Bean method.
大意:你不能直接在自定义的BeanPostProcessor或BeanFactoryPostProcessor内使用@Autowired,@Inject,@Value,@Resource注解;必须通过XML或者@Bean 方法的形式进行使用;如果这样用可能会出现各种奇葩问题,你只需知道不要这么用即可。