@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 ...
随机推荐
- Unified shader model
https://en.wikipedia.org/wiki/Unified_shader_model In the field of 3D computer graphics, the Unified ...
- cvc-complex-type.2.3: Element 'beans' cannot have character [children]
当启动spring的项目时,有时候会抛如下异常: Line 33 in XML document from ServletContext resource [/WEB-INF/backend-serv ...
- [图解tensorflow源码] 入门准备工作
tensorflow使用了自动化构建工具bazel.脚本语言调用c或cpp的包裹工具swig.使用EIGEN作为矩阵处理工具.Nvidia-cuBLAS GPU加速计算库.结构化数据存储格式prot ...
- 关于关闭TAB,IFRAME占用的内存不能释放问题
资料来源:http://jxd-zxf.iteye.com/blog/1440611 使用TAB时注意,如果TAB是引用IFRAME,关闭TAB时IFRAME不会被销毁从而导致内存不能释放,大量使用T ...
- WebDriverException: Message: f.QueryInterface is not a function
WebDriverException: Message: f.QueryInterface is not a function 使用webdriver打开c.highpin.cn,结果报错,见下图: ...
- ajax返回填充的数据不显示
原因:样式与id引用了其他的css或者js,删除其他样式,改变id就可以了
- 第五章 二叉树(e4)层次遍历
- TensorFlow—多层感知器—MNIST手写数字识别
1 import tensorflow as tf import tensorflow.examples.tutorials.mnist.input_data as input_data import ...
- 【校招面试 之 C/C++】第12题 C++ 重载、重写和重定义
1.成员函数重载特征: a.相同的范围(在同一个类中): b.函数名字相同: c.参数不同(参数个数不同或者参数类型不同,但是返回值不同不能使重载): d.virtual关键字可有可无. 2.重写 ...
- [leetcode]416. Partition Equal Subset Sum分割数组的和相同子集
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...