@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 ...
随机推荐
- tar 压缩文件指定目录
tar -cjf /app/tmp/app/test.tar.bz2 -C /app/tmp res_test.csv 将/app/tmp 目录下 res_test.csv文件压缩到/app/tmp ...
- Haskell语言学习笔记(36)Data.List.Zipper
ListZipper 模块 $ cabal install ListZipper Installed ListZipper-1.2.0.2 Prelude> :m +Data.List.Zipp ...
- break、continue、pass介绍
break.continue.pass介绍 break:跳出当前循环 continue:跳出本次循环,进行下一次循环 pass:什么也不做,占位.
- 条件语句;for循环 嵌套复习
//打印数字,0,1,8,10,12,每一个数单独占一行 //在全部数字打印完毕之后在打印数字的个数和所有数的和 int count = 0; int sum = 0; for (int i = 0; ...
- pycharm 远程开发
1. 服务器安装图形化 和 pycharm 本地使用 MobaXterm 工具登陆 session配置 勾选 x11-forwarding 运行pycharm.sh 2. 本地pycharm 远程服务 ...
- pip批量更新安装的包
------------------pip批量更新库-------------------- 1)查看过期的库 pip list --outdated 更新单一的库: pip install --u ...
- easyUIDataGrid分页
package com.cn.eport.util; import java.util.List; /** * * * @author zh * */ public class DataGrid im ...
- SO\PR回写的数据如下
insert into OUT_ORDER_RES ---JAVA FOR PR ) as LGORT ,'SAPRFC' as ERNAM,out_pr.due_datetime,out_pr.so ...
- mybatis什么时候需要声明jdbcType?
经常会见到以下两种写法:1. #{bookId}2. #{bookId,jdbcType=INTEGER}一般情况下,两种写法都可以.它们都可以获取Dao层传递过来的参数.但是,当传入的参数为null ...
- 破解版ps
http://www.sdifen.com/adobe-photoshop-cc.html