沈阳专业制作网站,网站子站怎么做、,网页制作平台排行榜,东莞网站页设计制作目录 IOC操作Bean管理XML方式#xff08;xml自动装配#xff09;
一.演示自动装配过程
#xff08;1#xff09;根据 属性名称 装配
步骤一#xff1a;
步骤二#xff1a;
步骤三#xff1a;
#xff08;2#xff09;根据 属性类型 装配 #xff08;2.1#…目录 IOC操作Bean管理XML方式xml自动装配
一.演示自动装配过程
1根据 属性名称 装配
步骤一
步骤二
步骤三
2根据 属性类型 装配 2.1根据 类型 自动装配产生的问题
解决方法 IOC操作Bean管理XML方式xml自动装配 手动装配 之前写的代码中可以往一个类中注入属性做法就是在xml配置文件中通过property标签中的name属性包括value属性值向类中设置值这种方式叫做 手动装配 自动装配 直接根据装配规则【属性名称或者属性类型】不需要明确指定是什么名称或者什么类型Spring自动将匹配的属性值进行注入这种方式叫做 自动装配 一.演示自动装配过程
1根据 属性名称 装配
步骤一
创建一个autowire包写入两个类员工类Employee类部门类Department类
结构如下 Employee类代码如下
package com.lbj.spring5.autowire;public class Employee {//写入部门对象//效果 在Employee中注入Department对象private Department department;public void setDepartment(Department department) {this.department department;}//加入toString方法方便输出Overridepublic String toString() {return Employee{ department department };}//加入测试方法方便测试public void test(){System.out.println(department);}}Department类
package com.lbj.spring5.autowire;public class Department {
} 步骤二 新建一个bean5.xml配置文件
分别在配置文件中将两个类的对象进行创建
然后根据属性名称自动注入
代码如下
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxmlns:utilhttp://www.springframework.org/schema/utilxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd!--实现自动装配bean 标签属性 autowire配置自动装配autowire属性常用两个值byName 根据属性名称注入注入的id值和类属性名称一样byType 根据属性类型注入
--bean idemployee classcom.lbj.spring5.autowire.Employee autowirebyName!--property namedepartment refdepartment/property--/beanbean iddepartment classcom.lbj.spring5.autowire.Department/bean/beans
测试类
package com.lbj.spring5.testdemo;import com.lbj.spring5.autowire.Employee;
import com.lbj.spring5.bean.Orders;
import com.lbj.spring5.collectiontype.Book;
import com.lbj.spring5.collectiontype.Course;
import com.lbj.spring5.collectiontype.Student;
import com.lbj.spring5.factorybean.Mybean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestSpring5Demo1 {Testpublic void tsetBean2(){ApplicationContext contextnew ClassPathXmlApplicationContext(bean5.xml);Employee employeecontext.getBean(employee, Employee.class);System.out.println(employee);}
}步骤三
测试结果 2根据 属性类型 装配
bean5.xml去Employee类中找到Department类的类型去装配 bean5.xml配置如下
?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:phttp://www.springframework.org/schema/pxmlns:utilhttp://www.springframework.org/schema/utilxsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd!--实现自动装配bean 标签属性 autowire配置自动装配autowire属性常用两个值byName 根据属性名称注入注入的id值和类属性名称一样byType 根据属性类型注入--bean idemployee classcom.lbj.spring5.autowire.Employee autowirebyType!--property namedepartment refdepartment/property--/beanbean iddepartment classcom.lbj.spring5.autowire.Department/bean/beans 测试结果 2.1根据 类型 自动装配产生的问题 根据 类型 自动装配时相同类型的bean对象不能定义多个否则类型识别不出来是哪个bean注入 如下所示 解决方法 1.使用 注解 2.使用 属性名称 装配