首先写两个类Dept类和Emp类。
Dept类:
package com.Keafmd.spring5.autowire; /** * Keafmd * * @ClassName: Dept * @Description: 部门类 * @author: 牛哄哄的柯南 * @date: 2021-01-16 13:43 */ public class Dept { @Override public String toString() { return "Dept{}"; } }
Emp类:
package com.Keafmd.spring5.autowire; /** * Keafmd * * @ClassName: Emp * @Description: 员工类 * @author: 牛哄哄的柯南 * @date: 2021-01-16 13:42 */ public class Emp { private Dept dept; // id和这里保持一致 public void setDept(Dept dept) { this.dept = dept; } @Override public String toString() { return "Emp{" + "dept=" + dept + '}'; } public void test(){ System.out.println(dept); } }
手动装配
手动装配的bean5.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="emp" class="com.Keafmd.spring5.autowire.Emp"> <!--手动装配--> <property name="dept" ref="dept"></property> </bean> <bean id="dept" class="com.Keafmd.spring5.autowire.Dept"></bean> </beans>
测试代码:
package com.Keafmd.spring5.testdemo; import com.Keafmd.spring5.autowire.Emp; import com.Keafmd.spring5.bean.Orders; import com.Keafmd.spring5.collectiontype.Book; import com.Keafmd.spring5.collectiontype.Course; import com.Keafmd.spring5.collectiontype.Stu; import com.Keafmd.spring5.factorybean.MyBean; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Keafmd * * @ClassName: TestSpring5demo1 * @Description: 测试类 * @author: 牛哄哄的柯南 * @date: 2021-01-15 14:30 */ public class TestSpring5demo1 { @Test public void test5(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml"); Emp emp = context.getBean("emp",Emp.class); System.out.println(emp); } }
测试结果:
Emp{dept=Dept{}} Process finished with exit code 0
自动装配
自动装配分为两种方式:根据属性名称自动注入,根据属性类型自动注入。
自动装配的配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--自动装配 bean标签属性 autowire ,配置自动装配 autowire属性常用的两个值: byName 根据属性名称自动注入 ,要注入的那个值bean的id值要和类里面属性名称一样 byType:根据属性类型自动注入 --> <bean id="emp" class="com.Keafmd.spring5.autowire.Emp" autowire="byType"> <!--手动装配--> <!-- <property name="dept" ref="dept"></property>--> </bean> <bean id="dept" class="com.Keafmd.spring5.autowire.Dept"></bean> <!--根据属性类型注入,多写这行就会报错,类型匹配到了两个bean,byName不会错--> <!-- <bean id="dept1" class="com.Keafmd.spring5.autowire.Dept"></bean>--> </beans>
测试代码相同:
package com.Keafmd.spring5.testdemo; import com.Keafmd.spring5.autowire.Emp; import com.Keafmd.spring5.bean.Orders; import com.Keafmd.spring5.collectiontype.Book; import com.Keafmd.spring5.collectiontype.Course; import com.Keafmd.spring5.collectiontype.Stu; import com.Keafmd.spring5.factorybean.MyBean; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Keafmd * * @ClassName: TestSpring5demo1 * @Description: 测试类 * @author: 牛哄哄的柯南 * @date: 2021-01-15 14:30 */ public class TestSpring5demo1 { @Test public void test5(){ ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml"); Emp emp = context.getBean("emp",Emp.class); System.out.println(emp); } }
测试结果:
Emp{dept=Dept{}} Process finished with exit code 0
这就是自动装配,但是这种基于xml自动装配并不常用,常用的是基于注解的。
以上就是基于xml的自动装配。