使用@Autowired注解装配bean
上一篇我们说到了在xml配置文件中装配bean,这种方法相对繁琐,灵活性不高,接下来我们看一下如何利用注解来快速装配bean。
@Autowired注解可以通过构造器、setter方法 以及字段(属性)自动装配bean.
首先创建一个People类作为将要被装配的bean,以及一个装配People的类Company如下所示:
people
public class People {
private String name;
public People(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Company
public class Company {
private People people;
public void setPeople(People people) {
this.people = people;
}
public People getPeople() {
return people;
}
}
设置xml文件,包含了一个people bean和一个company 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:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<bean id="company" class="com.yiwen.test5.Company"></bean>
<bean id="people1" class="com.yiwen.test5.People">
<!--这里通过property和constructor-arg同时设置一个属性name,最终name的值是property1而不是constructor1,注意一下-->
<property name="name" value="property1"></property>
<constructor-arg name="name" value="constructor1"></constructor-arg>
</bean>
</beans>
接下来就是利用注解的方式,将people bean注解到company中了。
通过setter方法注入
这里我们修改一下company的类,在setter上面写上@Autowired即可将对应的相同类型的bean(即people)注入到setter中public class Company { private People people; @Autowired public void setPeople(People people) { this.people = people; } public People getPeople() { return people; } }
测试结果
这里我们创建一个测试类,测试是否装配成功``` public class Test5 { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("test5.xml"); Company company=(Company)context.getBean("company") ; System.out.print( company.getPeople().getName()); } } ```
运行,输入结果为property1,表示已经将people bean装备进去了。
通过字段装配
这里还是修改了一下company类,在字段上加入@Autowired,可以用上一个测试类测试,结果相同。public class Company { @Autowired private People people; public void setPeople(People people) { this.people = people; } public People getPeople() { return people; } }
通过构造器装配
在构造器上加入@Autowired即可,可用上面的测试方法测试。public class Company { private People people; @Autowired public Company(People people) { this.people = people; } public void setPeople(People people) { this.people = people; } public People getPeople() { return people; } }
如果People在xml中有两个bean,那如何判断装配的是哪一个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:aop="http://www.springframework.org/schema/aop"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<bean id="company" class="com.yiwen.test5.Company"></bean>
<bean id="people1" class="com.yiwen.test5.People">
<property name="name" value="property1"></property>
<constructor-arg name="name" value="constructor1"></constructor-arg>
</bean>
<bean id="people2" class="com.yiwen.test5.People">
<property name="name" value="property2"></property>
<constructor-arg name="name" value="constructor2"></constructor-arg>
</bean>
</beans>
这时候,在利用@Autowired时就会报错,因为spring也不知道到底注入哪一个people的bean,所以这里我们就用添加一个新的注解@Qualifier ,xml文件如下所示:
public class Company {
private People people;
public Company(People people) {
this.people = people;
}
@Autowired
@Qualifier("people1")
public void setPeople(People people) {
this.people = people;
}
public People getPeople() {
return people;
}
}
这里我们在setter上添加了一个新注解@Qualifier(“people1”),表示使用的id为people1的people bean。
@Qualifier运用在构造方法上时,是注解在参数的前面,而不是构造方法之上,如下所示:
public class Company {
private People people;
@Autowired(required = false)
public Company( @Qualifier("people1") People people ) {
this.people = people;
}
public void setPeople(People people) {
this.people = people;
}
public People getPeople() {
return people;
}
}
依赖检查
默认情况下,@Autowired将执行相关检查,以确保属性已经装配正常。当Spring无法找到匹配的Bean装配,它会抛出异常。要解决这个问题,可以通过 @Autowired 的“required”属性设置为false来禁用此检查功能。 如下所示:
public class Company {
private People people;
public Company(People people) {
this.people = people;
}
@Autowired(required = false)
@Qualifier("people1")
public void setPeople(People people) {
this.people = people;
}
public People getPeople() {
return people;
}
}
总结
注解功能优于配置文件装配bean,在工作中大多数遇到的是使用注解的方法注入bean。注解还有很多功能我将在接下来的文章中和大家一起分享。