@Autowired注解是通过匹配数据类型自动装配Bean
创建Customer.java,可以给属性person、带person的构造器、setPerson方法任意一个标记@Autowired
package com.Re.autowiredDemo;
import org.springframework.beans.factory.annotation.Autowired;
public class Customer {
//新增一个adress参数
private Address address;
//给person使用@Autowired装配
@Autowired
private Person person;
//无参构造
public Customer() {
}
//带参构造
public Customer(Person person) {
this.person = person;
}
public Customer(Address address) {
this.address = address;
}
public void setPerson(Person person) {
this.person = person;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Customer{" +
"address=" + address +
", person=" + person +
'}';
}
}
创建Person.java
package com.Re.autowiredDemo;
public class Person {
private String hei;
private String hi;
public String getHei() {
return hei;
}
public void setHei(String hei) {
this.hei = hei;
}
public String getHi() {
return hi;
}
public void setHi(String hi) {
this.hi = hi;
}
@Override
public String toString() {
return "Person{" +
"hei='" + hei + '\'' +
", hi='" + hi + '\'' +
'}';
}
}
创建测试文件Test.java
package com.Re.autowiredDemo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
Customer customer = (Customer) context.getBean("customer");
System.out.println(customer);
}
}
编辑配置文件“spring-config.xml”
<?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.xsd">
<!--指定文件路径-->
<bean id="helloBean" class="com.Re.beanDemo.HelloSpring">
<!--通过property给参数赋值 -->
<property name="name" value="Re"/>
</bean>
<!--<!–默认方式装配,有两个bean:customer和person-->
<!--property name="person"指向Customer.setPerson方法-->
<!--ref="person"指向"<bean id="person" class="com.Re.autowiredDemo.Person"/>"–>-->
<!--<bean id="customer" class="com.Re.autowiredDemo.Customer">-->
<!--<property name="person" ref="person"/>-->
<!--</bean>-->
<!--<bean id="person" class="com.Re.autowiredDemo.Person">-->
<!--<!–将“hello”赋值给参数“hei”–>-->
<!--<property name="hei" value="hello"/>-->
<!--</bean>-->
<!--<!–byName方式自动装配–>-->
<!--<bean id="customer" class="com.Re.autowiredDemo.Customer" autowire="byName"/>-->
<!--<bean id="address2" class="com.Re.autowiredDemo.Address">-->
<!--<property name="myaddress" value="杭州西湖"/>-->
<!--</bean>-->
<!--<!–通过类型自动装配–>-->
<!--<bean id="customer" class="com.Re.autowiredDemo.Customer" autowire="byType"/>-->
<!--<bean id="address2" class="com.Re.autowiredDemo.Address">-->
<!--<property name="myaddress" value="杭州余杭"/>-->
<!--</bean>-->
<!--这一行配置是启用@Autowired
也可用<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>-->
<context:annotation-config/>
<!--通过@Autowired自动装配-->
<bean id="customer" class="com.Re.autowiredDemo.Customer"/>
<bean id="person" class="com.Re.autowiredDemo.Person">
<property name="hei" value="hello autowired"/>
</bean>
</beans>
项目结构
image.png
注释掉配置文件里person的配置
会报错,原因是@Autowired会检查属性已经装配正常。当Spring无法找到匹配的Bean装配,就会报错。
可以通过 @Autowired 的“required”属性设置为false来禁用此检查功能。
image.png
image.png
image.png
如果有多个person bean配置,那么要用@Qualifier注解指定使用哪个bean
image.png
image.png
网友评论