@Autowired @Resource @Inject 自动注入
一、@AutoWired ( spring 的注解 )自动注入
/**
* @Autowired:
* 默认按照 Student 类型去容器中找对应的组件:applicationContext.getBean(Student.class);
* 如果找到多个相同类型的组件,再将 student 这个属性名作为 id 去容器中找对应组件 applicationContext.getBean("student");
* required = false,容器中如果没有该组件,就为 null
* @Qualifier:
* 指定需要装配的组件 id,而不是使用 student 这个属性名作为 id
*/
@Qualifier("student2")
@Autowired(required = false)
private Student student;
二、@Resource ( JSR250 规范的注解 )
/**
* @Resource:
* 默认按照组件名 student 作为 id 去容器中找对应的属性
* 使用 name = "student2" 指定 id
* 没有 required = false 功能
*/
@Resource(name = "student2")
private Student student;
三、@Inject ( JSR330 规范 ) 和 @AutoWired 功能一样。但是没有 required = false 功能
导入 jar
<!-- https://mvnrepository.com/artifact/javax.inject/javax.inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
@Inject
private Student student;
@Autowired @Resource @Inject 自动注入的更多相关文章
- spring boot 中@Autowired注解无法自动注入的错误
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/huihuilovei/article/de ...
- 搭建SSH框架整合Struts2和Spring时,使用@Autowired注解无法自动注入
© 版权声明:本文为博主原创文章,转载请注明出处 1.问题描述: 搭建SSH框架,在进行Struts2和Spring整合时,使用Spring的@Autowired自动注入失败,运行报错java.lan ...
- 21、自动装配-@Resource&@Inject
21.自动装配-@Resource&@Inject Spring 还支持使用@Resource(JSR250)和@Inject(JSR330)[Java规范的注解] AutowiredAnno ...
- @Autowired @Resource @Qualifier的区别
参考博文: http://www.cnblogs.com/happyyang/articles/3553687.html http://blog.csdn.net/revent/article/det ...
- @Autowired @Resource用法
@Autowired的用法和作用 这个注解就是spring可以自动帮你把bean里面引用的对象的setter/getter方法省略,它会自动帮你set/get. <bean id="u ...
- Spring自动注入有关的注解
Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. 1,@Comp ...
- spring bean自动注入
使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的 ...
- SpringMVC常用注解@Controller,@Service,@repository,@Component,@Autowired,@Resource,@RequestMapping
1.controller层使用@Controller注解-用于呈现层,(spring-mvc) @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controlle ...
- Spring自动注入之@Autowired、@Resource、@Inject
相同点: 三者都支持对spring bean的自动注入 不同点: ①Autowired按照类型进行注入( Bean bean = applicationContext.getBean(Bean.cla ...
随机推荐
- linux c 操作utmp 和 wtmp 文件接口
/var/run/utmp 保存当前在本系统中的用户信息 /var/log/wtmp 保存登陆过本系统的用户信息 他们保存的信息是基于结构体 struct utmp 的(/usr/include/bi ...
- Sql Server 强制断开数据库已有连接的方法
用管理员账户sa登陆,然后在master下新建查询: 在查询窗体输入: declare @i int declare cur cursor for select spid from sysproces ...
- 准备开源用javascript写Tomcat下的WebApp的项目
原创文章,转载请注明. 这个想法由来已久.用javascript编写Tomcat下的WebApp.现现在也有alpha版本号的实现. 这种话,前端程序猿就能够像用Node.js那样,用javascri ...
- <LeetCode OJ> 326. Power of Three
326. Power of Three Question Total Accepted: 1159 Total Submissions: 3275 Difficulty: Easy 推断给定整数是否是 ...
- luogu3769 【模板】AC自动机(加强版)
题目大意:有N个由小写字母组成的模式串以及一个文本串T.每个模式串可能会在文本串中出现多次.你需要找出哪些模式串在文本串T中出现的次数最多. 对每个模式串建立一个Trie树.定义一个节点的Fail指针 ...
- camera table表编译
mmm -j8 vendor/mediatek/proprietary/hardware/mtkcam/v1/common/paramsmgr/ 2>&1 | tee ft.lib.lo ...
- 【POJ 1222】 EXTENDED LIGHTS OUT
[题目链接] http://poj.org/problem?id=1222 [算法] 列出异或方程组,用高斯消元求解即可 [代码] #include <algorithm> #includ ...
- Hadoop MapReduce编程 API入门系列之倒排索引(二十四)
不多说,直接上代码. 2016-12-12 21:54:04,509 INFO [org.apache.hadoop.metrics.jvm.JvmMetrics] - Initializing JV ...
- History of the browser user-agent string--转
https://webaim.org/blog/user-agent-string-history/ In the beginning there was NCSA Mosaic, and Mosai ...
- Solr.NET快速入门(二)
字典映射和动态字段 Solr dynamicFields可以根据用例不同地映射. 它们可以被"静态地"映射,例如,给定: <dynamicField name="p ...