1.xml配置文件(了解)
1.1 byName 按名称自动装配(推荐,需要get和set方法)
根据类型进行自动装配. 但要求 IOC 容器中只有一个类型对应的 bean, 若有多个则无法完成自动装配
<bean id="student" class="com.spring.autowire.Student" autowire="byName"> <property name="stuName" value="小魔仙"></property> </bean> <bean id="address" class="com.spring.autowire.Address"> <property name="addressInfo" value="一中"></property> </bean>
1.2 btType 按类型自动装配
若属性名和某一个 bean 的 id 名一致, 即可完成自动装配. 若没有 id 一致的, 则无法完成自动装配
<bean id="student" class="com.spring.autowire.Student" autowire="byType"> <property name="stuName" value="小魔仙"></property> </bean> <bean id="address" class="com.spring.autowire.Address"> <property name="addressInfo" value="一中"></property> </bean>
1.3 全局autowire
<?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:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-autowire="byName"> </beans>
2.注解装配(熟悉)
2.1 @Autowired
@Autowired是按类型自动转配的
@Autowired private School school;
<bean id="school" class="com.spring.autowire.School"> <property name="schoolName" value="一中"></property> </bean>
2.2 @Qualifier
@Autowired是根据类型自动装配的,加上@Qualifier则可以根据byName的方式自动装配,其中@Qualifier不能单独使用
@Autowired @Qualifier("schoolName") private School school;
<bean id="schoolName" class="com.spring.autowire.School"> <property name="schoolName" value="一中"></property> </bean>
2.3 @Resource
@Resource如有指定的name属性,先按该属性进行byName方式查找装配;其次再进行默认的byName方式进行装配;如果以上都不成功,则按byType的方式自动装配。都不成功,则报异常
@Resource private School school;
<bean id="school" class="com.spring.autowire.School"> <property name="schoolName" value="一中"></property> </bean>
或者
@Resource(name="schoolName") private School school;
<bean id="schoolName" class="com.spring.autowire.School"> <property name="schoolName" value="一中"></property> </bean>
完整Demo
package com.spring.autowire; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; /** * 学生类 * * @author yyx 2019年6月9日 */ public class Student { /** * 学生姓名 */ private String stuName; /** * 学校信息 */ @Autowired private School school; /** * 地址信息 */ private Address address; public String getStuName() { return stuName; } public void setStuName(String stuName) { this.stuName = stuName; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } @Override public String toString() { return "Student [stuName=" + stuName + ", school=" + school + ", address=" + address + "]"; } }
package com.spring.autowire; /** * 地址类 * * @author yyx 2019年6月9日 */ public class Address { /** * 地址信息 */ private String addressInfo; public String getAddressInfo() { return addressInfo; } public void setAddressInfo(String addressInfo) { this.addressInfo = addressInfo; } @Override public String toString() { return "Address [addressInfo=" + addressInfo + "]"; } }
package com.spring.autowire; /** * 学校类 * * @author yyx 2019年6月9日 */ public class School { /** * 学校名称 */ private String schoolName; public String getSchoolName() { return schoolName; } public void setSchoolName(String schoolName) { this.schoolName = schoolName; } @Override public String toString() { return "School [schoolName=" + schoolName + "]"; } }
package com.spring.autowire; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * 测试自动装配 * * @author yyx 2019年6月9日 */ public class AutowireMain { public static void main(String[] args) { ApplicationContext aContext = null; try { aContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Student student = (Student) aContext.getBean("student"); System.out.println(student); } catch (Exception e) { } } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" 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:component-scan base-package="com.spring.autowire" resource-pattern="com.spring.autowire.Student"></context:component-scan> <!-- byType: 根据类型进行自动装配. 但要求 IOC 容器中只有一个类型对应的 bean, 若有多个则无法完成自动装配. byName: 若属性名和某一个 bean 的 id 名一致, 即可完成自动装配. 若没有 id 一致的, 则无法完成自动装配 --> <bean id="student" class="com.spring.autowire.Student" autowire="byName"> <property name="stuName" value="小魔仙"></property> </bean> <bean id="address" class="com.spring.autowire.Address"> <property name="addressInfo" value="湖口"></property> </bean> <bean id="school" class="com.spring.autowire.School"> <property name="schoolName" value="一中"></property> </bean> </beans>