@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 ...
随机推荐
- Kafka Manager 监控
1.安装: 依赖java环境,须首先安装java运行环境,并正确设置路径. 确保kafka已经安装,且版本合适. 修改配置文件: kafka-manager.zkhosts="你的zoo ...
- int和Integer区别
Java是一个近乎纯洁的面向对象编程语言,但是为了编程的方便还是引入了基本数据类型,但是为了能够将这些基本数据类型当成对象操作,Java为每一个基本数据类型都引入了对应的包装类型(wrapper cl ...
- Boost.Coroutine2:学习使用Coroutine(协程)
function(函数)routine(例程)coroutine (协程) 函数,例程以及协程都是指一系列的操作的集合. 函数(有返回值)以及例程(没有返回值)也被称作subroutine(子例程), ...
- session第二篇
二 A.application对象 1.application对象实现了用户间数据的共享,可存放全局变量. 2.application对象开始于服务器的启动,终止于服务器的关闭. 3.在用户的前后连接 ...
- RN中关于组件中属性的传递
比如: 组件A想要给组件B中的组件C传递一个属性prop class A extends Component{ render(){ return( <B title = "这是一个标题 ...
- 在UNITY中按钮的高亮用POINT灯实现,效果别具一番风味
在UNITY中按钮的高亮用POINT灯实现,效果别具一番风味
- ArrayList LinkList比较
1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,ArrayList优于LinkedList,因为ArrayLi ...
- Spring配置连接池
---------------------siwuxie095 Spring 配置连接池 1.Spring 配置内置连接 ...
- C#.net随机数函数
(1)Random rnd = new Random(); int rndNum = rnd.Next(); //int 取值范围内的随机数 int rndNum = rnd.Ne ...
- centos7下mysql5.6的主从复制
一.mysql主从复制介绍 mysql的主从复制并不是数据库磁盘上的文件直接拷贝,而是通过逻辑的binlog日志复制到要同步的服务器本地,然后由本地的线程读取日志里面的sql语句,重新应用到mysql ...