【Spring】@Autowired and @Resource
来源:互联网 发布:js 设置input编辑状态 编辑:程序博客网 时间:2024/06/06 03:51
Given that Spring can autowire either by type or by name, it is always confusing to me that what is the default behavior when using annocation @Autowired or @Resource to do the autowire. Therefore, I made an experiment to help clear out confusion. Here are some of my findings:
1. By default @Autowired and @Resource will first use field name or parameter name to autowire bean with the same name as bean id. If it can't find any matching bean id, then it will autowire by type.
2. When autowire by type, for certain type, there must be only one bean existing in the application context. Otherwise, Spring will throw exception like: No unique bean of type [com.ms.junz.domain.User] is defined: expected single matching bean but found 2: [user1, user]
3. If your field name is independent of bean id and there might be multiple beans with the same type, you can use either@Qualifier("beanId") together with @Autowired, or @Resource(name="beanId") to explicitly tell Spring which bean to autowire. Then, Spring wouldn't complain any longer.
- 【Spring】@Autowired and @Resource
- Spring Injection with @Resource, @Autowired and @Inject
- Spring Injection with @Resource, @Autowired and @Inject
- SPRING INJECTION WITH @RESOURCE, @AUTOWIRED AND @INJECT
- Spring注释@Autowired @Resource
- Spring @Autowired @Resource注解
- spring Resource Autowired
- spring @Resource @Autowired
- Spring注释@Autowired @Resource
- Spring - @Resource与@Autowired
- [Spring] @AutoWired @Resource 区别
- spring @Resource @Autowired 区别
- Spring----@Resource和@Autowired
- spring 注解@Resource 和 @Autowired
- spring自动装配@autowired,@Resource
- Spring 注解 @Resource和@Autowired
- spring @Autowired vs @Resource 区别
- Spring 注解 @Resource和@Autowired
- 如何将FLex AIR运行环境与AIR程序一起打包
- 黑马程序员__关于学习Java中IO流的FileWriter的总结
- NYOJ - 整数中的1
- No tag "datetimepicker" defined in tag library imported with prefix "s"解决
- <<Think in Java 4 >>中关于使用XOM操作xml文件
- 【Spring】@Autowired and @Resource
- ruby on rails 问题解决
- 母函数学习
- hdu1272 小希的迷宫 hdu1856 More is better
- 【Hibernate】 Performance Tuning
- 黑马程序员_面试题--交通灯总结
- Android读取SD卡下面所有的TXT文件名 listView显示出来
- 我的学习生涯(Delphi篇) - 06
- Linux Netcat 命令——网络工具中的瑞士军刀