当前栏目
Spring依赖注入@Autowried的这些功能你都知道吗?
环境: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 方法的形式进行使用;如果这样用可能会出现各种奇葩问题,你只需知道不要这么用即可。
相关文章
- QT5.x开发环境配置
- Android(4.0.3+): Service, AsyncTask, 定时任务和UI通信
- 使用nmap查看web服务支持的http methods
- 用Javascript判断访问来源操作系统, 设备, 浏览器类型
- Nginx 启用 https
- Firefox访问https出现 ssl_error_weak_server_ephemeral_dh_key错误
- PHP提升echo, printf, print, file_put_contents等输出方法的效率
- Apache配置中的ProxyPass 和 ProxyPassReverse
- Centos5.8 安装SVN并配置HTTP访问
- CentOS RHEL 安装 Tomcat 7
- 提速 10 倍!深度解读字节跳动新型云原生 Spark History Server
- CSS 边框属性总结
- Springboot整合Rabbitmq,Direct、Fanout、Topic
- 仿站小工具和小飞兔(扒取网站页面)
- JQuery JavaScript常用API整理(前端入门必学)
- SrpingCloud Tencent Polaris中的组件
- Node.js安装
- 前端开发与构建工具Vite
- OkHttp搞定Http请求
- OpenSCA用开源的方式做开源风险治理:Why? What? How?