@Autowired 和 @Qualifier
一 无冲突
bean工厂
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"> <!-- here1 -->
<context:annotation-config /> <beans>
<bean id="person" class="cn.zno.hello.Person" >
<property name="name" value="XiaoMing"></property>
<property name="age" value="22"></property>
<property name="savings" value="10000"></property>
</bean> <bean id="car" class="cn.zno.hello.Car">
<property name="brand" value="BYD"></property>
<property name="price" value="54000"></property>
</bean>
</beans> </beans>
测试主函数
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("Beans.xml");
Person person = (Person) ctx.getBean("person");//here2
// Person person = new Person(); //here3
System.out.println(person);
}
javebean 片段
public class Person {
private String name;
private int age;
private double savings;
@Autowired
private Car car;
public void say(){
System.out.println("Hello World!");
}
...
说明:
here1 开启自动注入
here3 通过 new 的方式无法自动注入
here2 从bean工厂取的bean可以自动注入
二有冲突
package cn.zno.testmybatis;
public class QualifierBean {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<bean id="a" class="cn.zno.testmybatis.QualifierBean">
<property name="name" value="a1"></property>
</bean>
<bean id="b" class="cn.zno.testmybatis.QualifierBean">
<property name="name" value="b1"></property>
</bean>
单元测试:
@Autowired @Qualifier("a")
private QualifierBean qualifierBean1;
@Autowired @Qualifier("b")
private QualifierBean qualifierBean2;
注意:
@Qualifier 必须配合 @Autowired ,否则无法注入
@Qualifier 指定的值必须在<bean 中通过 id 或 name 指定,不存在时会抛异常(id 和 name 可同时存在且值不同,且都可以通过@Qualifier 指定)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [cn.zno.testmybatis.QualifierBean] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=a)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 28 more
id 和 name 不能与其他bean 的 id 和 name 相同
Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Bean name 'b' is already used in this <beans> element
三静态注入
@Component
public class Foo { public void say(String s) {
System.out.println(s);
}
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; @Component
public class SomeUtils { private static Foo foo; @Autowired
public void setFoo(Foo foo) {
SomeUtils.foo = foo;
} public static void doSomeThing() {
foo.say("1111");
} }
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class ZTestsetApplication { public static void main(String[] args) {
SpringApplication.run(ZTestsetApplication.class, args); SomeUtils.doSomeThing();
} }
@Autowired 和 @Qualifier的更多相关文章
- Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier、@scope
以下内容摘自部分网友的,并加上了自己的理解 @Service用于标注业务层组件(我们通常定义的service层就用这个) @Controller用于标注控制层组件(如struts中的action.Sp ...
- Spring 学习——Spring常用注解——@Component、@Scope、@Repository、@Service、@Controller、@Required、@Autowired、@Qualifier、@Configuration、@ImportResource、@Value
Bean管理注解实现 Classpath扫描与组件管理 类的自动检测与注册Bean 类的注解@Component.@Service等作用是将这个实例自动装配到Bean容器中管理 而类似于@Autowi ...
- @Resource、@Autowired、@Qualifier 区别(表格显示)
@Resource.@Autowired.@Qualifier 区别(表格显示) 区别项 @Resource @Autowired @Qualifier 谁提供的 jdk提供,包是javax.anno ...
- @Autowired @Resource @Qualifier的区别
参考博文: http://www.cnblogs.com/happyyang/articles/3553687.html http://blog.csdn.net/revent/article/det ...
- Spring注解 @Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析
@Repository.@Service.@Controller 这几个是一个类型,其实@Component 跟他们也是一个类型的 Spring 2.5 中除了提供 @Component 注释外,还定 ...
- @autowired、@Qualifier、@Primary注解
@autowired 可以自动帮你把Bean里面引用的对象的setter/getter方法省略,自动帮你set/get. 启动spring IoC时,容器自动装载了一个AutowiredAnnotat ...
- Spring注解之@Autowired、@Qualifier、@Resource、@Value
前言 @Autowired.@Qualifier.@Resource.@Value四个注解都是用于注入数据的,他们的作用就和在xml配置文件中的bean标签中写一个标签的作用是一样的!本篇中特别要讲解 ...
- 关于Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析
1.Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service和 @Controller 其实这三个跟@Com ...
- @Resource、@Autowired、@Qualifier的注解注入及区别
在Java代码中可以使用 @Resource 或者 @Autowired 注解方式来进行注入. 虽然 @Resource 和 @Autowried 都可以完成依赖注入,但是他们是有区别的. 一: @ ...
- Spring @Resource、@Autowired、@Qualifier的区别
@Resource默认是按照名称来装配注入的,只有当找不到与名称匹配的bean才会按照类型来装配注入: @Resource注解是J2EE提供,而@Autowired是由Spring提供,故减少系统对s ...
随机推荐
- WP8.1 页面跳转,overwrite后退键
In 8.1 we use the below code to navigate between pages: this.Frame.Navigate(typeof(PivotPage)); In 8 ...
- React 简单介绍
React 简单介绍 作者 RK_CODER 关注 2014.12.10 17:37* 字数 2516 阅读 55715评论 6喜欢 70 why React? React是Facebook开发的一款 ...
- js脚本代码调试小技巧
以前写js代码调试代码查看数据是否正确的时候不知道F12(开发者工具),都是alert(xxx)或者console.log(xxx), 现在知道还可以用document.write或者try...ca ...
- 代码报错记录-MAVEN-2
报错: 编译错误,程序包org.junit找不到 原因: 这个是父项目,报错是在子项目中,子项目使用了父项目的junit包,由于scope是test,导致子项目在编译时找不到junit, 修改: 将父 ...
- C++连接Oracle之OCCI(windows)
上一节我们讲过了ADO连接Oracle,这一节我们尝试通过OCCI的方式,来在windows平台下连接Oracle数据库,下一节讨论在Linux环境下通过OCCI的方式连接远程的Oracle数据库. ...
- How to Pronounce UMBRELLA
How to Pronounce UMBRELLA Share Tweet Share Tagged With: 3-Syllable When the weather is bad, you’ll ...
- js中怎么写自执行函数
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- innerText 与textContent区别
两者都是可以过滤html元素 innerText 获取内容 换行会有L类似特殊符号 textContent 没有
- eclipise快捷键,留给以后备用
快捷键无效解决办法: 1.考虑是否被其他应用占用,如QQ,QQ音乐,千千动听等 2.在eclispe查看是否被修改:Window->Preferences->General->Key ...
- avalon.js 文字显示更多与收起
isShowMore: function (content) { if(content && content.length >= 124){ var shortContent = ...