[Spring]@Autowired,@Required,@Qualifier注解
@Required注解
@Required注解用于setter方法,表明这个属性是必要的,不可少的,必须注入值
假设有个测试类,里面有name和password两个属性
我给两个属性的setter方法都加了@Required注解
package com.example.demo1.Implements; import com.example.demo1.Interface.UserService;
import org.springframework.beans.factory.annotation.Required; public class UserClientImpl implements UserService { private String name;
private String password; public UserClientImpl(){} public UserClientImpl(String name,String password){
this.name = name;
this.password = password;
} public String getName() {
return name;
} @Required
public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} @Required
public void setPassword(String password) {
this.password = password;
} @Override
public void save() {
System.out.println("客户端保存信息"+name+"--"+password);
}
}
现在我只给一个属性加注入,另一个不加
可以看到报错

然后我补上注入之后就没问题了

@Autowoired注解
其实看名字就可以看出来,这个是跟自动装填有关
使用它需要加一行代码
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcess></bean>
1,在属性前加此注解
先给定两个类
package com.example.demo1.Other;
public class CustomerTest {
public CustomerTest(){
System.out.println("在Customer.构造方法中...");
}
public void show(){
System.out.println("在Customer.show方法中...");
}
}
package com.example.demo1.Implements; import com.example.demo1.Interface.Customer;
import com.example.demo1.Other.CustomerTest;
import org.springframework.beans.factory.annotation.Autowired; public class CustomerImpl implements Customer { private String name;
private String id;
@Autowired
private CustomerTest customerTest; public CustomerTest getCustomerTest() {
return customerTest;
} public void setCustomerTest(CustomerTest customerTest) {
this.customerTest = customerTest;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
} @Override
public void show() {
System.out.println(id+"..."+name);
customerTest.show();
}
}
第二个类在第三个成员变量前面加个此注解
然后applicationContext这样写
<bean id="Customer"
class="com.example.demo1.Implements.CustomerImpl">
</bean>
<bean id="CustomerTest" class="com.example.demo1.Other.CustomerTest"></bean>
在打印一下结果
ApplicationContext instance = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerImpl customer = (CustomerImpl) instance.getBean("Customer");
customer.show();
((ClassPathXmlApplicationContext) instance).registerShutdownHook();
可以看到Customer对象是自动装填了的
2,在构造函数之前加此注解
效果和上面是一样的,不演示了
3,@Autowired(required=false)的作用
这里跟之前的@Required的作用类似
默认情况下,@Autowired 注释意味着依赖是必须的,它类似于 @Required 注释,然而,你可以使用 @Autowired 的 (required=false) 选项关闭默认行为。
这里跟@Required的效果类似,不演示了
@Qualifier注解
在之前的学习注解的过程中,显然,用自动装配的时候,如果配置的bean是相同的类的生成的对象,会报错,于是为了解决这个问题,@Qualifier就出来了
@Qualifier和@Autowired一起使用,在@Qualifier后面的括号里欲装配的bean的名称,就可以让Spring自动给我们装配合适的bean
首先applicationContext.xml ,因为要用到context标签,需要用到aop.jar,所以在这之前记得引入Springframework-aop.jar,这个标签就像之前的注解一样,需要注册那几个Processor才能起作用
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:annotation-config/>
<bean id="Profile" class="com.example.demo1.Testclass.Profile"></bean> <bean id="student1" class="com.example.demo1.Testclass.Student">
<property name="name" value="student1"></property>
<property name="age" value="2"></property>
</bean>
<bean id="student2" class="com.example.demo1.Testclass.Student">
<property name="age" value="3"></property>
<property name="name" value="student2"></property>
</bean>
</beans>
注解是放在要装配放属性的上方
package com.example.demo1.Testclass; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; public class Profile {
@Autowired
@Qualifier(value = "student1")
private Student student;
public Profile(){
System.out.println("Inside Profile constructor." );
}
public void setStudent(Student student) {
this.student = student;
} public void printAge() {
System.out.println("Age : " + student.getAge() );
}
public void printName() {
System.out.println("Name : " + student.getName() );
}
}
package com.example.demo1.Testclass;
public class Student {
private Integer age;
private String name;
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
之后Spring就会识别出合适的bean并注入了
测试代码
ApplicationContext instance = new ClassPathXmlApplicationContext("applicationContext.xml");
Profile profile = (Profile) instance.getBean("Profile");
profile.printName();
((ClassPathXmlApplicationContext) instance).registerShutdownHook();
结果也正是这样

[Spring]@Autowired,@Required,@Qualifier注解的更多相关文章
- Java Spring各种依赖注入注解的区别
Spring对于Bean的依赖注入,支持多种注解方式: @Resource javax.annotation JSR250 (Common Annotations for Java) @Inject ...
- 详解Java Spring各种依赖注入注解的区别
注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired.Resource.Qualifier.Service.Controller.Repository.Comp ...
- Spring 学习——Spring常用注解——@Component、@Scope、@Repository、@Service、@Controller、@Required、@Autowired、@Qualifier、@Configuration、@ImportResource、@Value
Bean管理注解实现 Classpath扫描与组件管理 类的自动检测与注册Bean 类的注解@Component.@Service等作用是将这个实例自动装配到Bean容器中管理 而类似于@Autowi ...
- Spring @Resource、@Autowired、@Qualifier的注解注入及区别
spring2.5提供了基于注解(Annotation-based)的配置,我们可以通过注解的方式来完成注入依赖.在Java代码中可以使用 @Resource或者@Autowired注解方式来经行注入 ...
- 关于Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析
1.Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service和 @Controller 其实这三个跟@Com ...
- Spring注解@Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier、@scope
以下内容摘自部分网友的,并加上了自己的理解 @Service用于标注业务层组件(我们通常定义的service层就用这个) @Controller用于标注控制层组件(如struts中的action.Sp ...
- Spring @Autowired、@Resource、@Required、@Component、@Repository、@Service、@Controller注解的用法和作用
Spring @Autowired,@Resource,@Required注解的用法和作用 Spring中 @Autowired标签与 @Resource标签 的区别 Spring注解@Compone ...
- Spring注解 @Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析
@Repository.@Service.@Controller 这几个是一个类型,其实@Component 跟他们也是一个类型的 Spring 2.5 中除了提供 @Component 注释外,还定 ...
- Spring注解之@Autowired、@Qualifier、@Resource、@Value
前言 @Autowired.@Qualifier.@Resource.@Value四个注解都是用于注入数据的,他们的作用就和在xml配置文件中的bean标签中写一个标签的作用是一样的!本篇中特别要讲解 ...
随机推荐
- oracle listagg within group
案例: 查看,每个人身上的标签. 1)表数据 2)SQL select name,listag(tag,',') within group(order by tag) tags from table_ ...
- 2018.12.17 bzoj3667: Rabin-Miller算法(Pollard-rho)
传送门 Pollard−rhoPollard-rhoPollard−rho板题. 题意简述:给出几个数,让你判断是不是质数,如果不是质数就求出其最大质因子,数的大小为1e181e181e18以内. 先 ...
- tomcat7 安装 windows 服务
tomcat 可以安装成windows 服务,这样 每次启动就不需要启动tomcat了. 具体配置: 1.修改 service.bat 在行首添加 set "JAVA_HOME=E:\jdk ...
- UVa 11762 Race to 1 (数学期望 + 记忆化搜索)
题意:给定一个整数 n ,然后你要把它变成 1,变换操作就是随机从小于等于 n 的素数中选一个p,如果这个数是 n 的约数,那么就可以变成 n/p,否则还是本身,问你把它变成 1 的数学期望是多少. ...
- 解决Linux下IDEA无法使用ibus输入法的问题和tip乱码
一:可以先按网上的配置/etc/profile里的输入法的一些参数,我是先配置了这些参数的,但是输入法还是没用,后来一直没管它了,今天用了一些方式可以了但不敢保证不需要先配置那些参数: 二:情况:开启 ...
- shell 脚本学习之read
Read的一些选项 Read可以带有-a, -d, -e, -n, -p, -r, -t, 和 -s八个选项. -a :将内容读入到数值中 echo -n 'please enter:'read -a ...
- weblogic配置集群(二)
接到上一篇博文来 weblogic配置集群(一) 三.启动管理server 按照上面的方式创建好域后,在D:\Oracle\Middleware\user_projects\domains\gszhD ...
- excel双击文件打开时空白,使用菜单打开正常的解决办法
excel双击文件打开时空白,使用菜单打开正常的解决办法 打开 excel的选项 找到高级->常规 取消“忽略使用动态数据交换...”的勾,确定. 问题解决.
- Ng第十五课:异常检测(Anomaly Detection)
15.1 问题的动机 15.2 高斯分布 15.3 算法 15.4 开发和评价一个异常检测系统 15.5 异常检测与监督学习对比 15.6 选择特征 15.7 多元高斯分布(可选) 15 ...
- 2017-12-24 自定义view相关学习
学习材料: http://blog.csdn.net/u010661782/article/details/52805870 http://blog.csdn.net/chengyingzhilian ...