定义员工类,类名EMP
public class Emp(){
private String ename;
private String gender;
private String dept;
public Dept getDept(){
return dept;
}
private void setDept(Dept dept){
this.dept = dept;
}
public void setEname(String ename){
this.ename = ename;
}
public void setGender(String gender){
this.gender = gender;
}
public void add(){
System.out.println("姓名" + ename + "\n" + "性别" + gender + "\n" + "部门" + dept);
}
}
定义Dept部门类
public class Dept(){
private String dname;
public void setDname(String dname){
this.dname = dname;
}
@Override
public String toString() {
return "Dept{" +
"dname='" + dname + '\'' +
'}';
}
}
```xml
创建bean1.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-2.0.xsd">
<bean id="emp" class="com.xuanniao.emp">
<property name="ename" value="Chosen1"></property>
<property name="gender" value="男"></property>
<property name="dept">
<bean id="dept" class="com.xuanniao.dept">
<property name="dname" value="技术部"></property>
</bean>
</property>
</bean>
</beans>
测试方法
public void HelloTest(){
@Test
public void emp(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Emp emp = context.getBean("emp",Emp.class);
emp.add();
com.xuanniao.Dao.Emp@7dcf94f8
}
}
IOC操作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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="emp" class="com.xuanniao.emp">
//先定义两个Emp对象值
<property name="ename" value="luck"></property>
<property name="gender" value="男"></property>
//级联赋值
<property name="dept" ref="dept"></property>
</bean>
<bean id="dept" class="com.xuanniao.dept">
//这里修改数据,技术部变成财务部
<property name="dname" value="财务部"></property>
</bean>
</beans>
使用Bean完成集合类型属性注入
public class Stu(){
1.数组属性的注入
private String[] courses;
2.List类型属性注入
private List<String> list;
3.map集合属性注入
private Map<String,String> map;
4.set类型属性集合
private Set<String> Stringset;
5.学生所学的多门课程
private List<course> courselist;
public void setCourse(List<Course> courseList){
this.courseList = courseList;
}
public void setCourses(String[] courses){
this.courses = courses;
}
public void setList(List<String> list){
this.list = list;
}
public void setMap(Map<String,String> map){
this.map = map;
}
public void setSet(Set<String> Stringset){
this.Stringset = Stringset;
}
public void Chosen1(){
System.out.println(courses + list + map + Stringset + courselist);
}
}
}
public class Course(){
private String cname;
public void setCname(String cname){
this.cname = cname;
}
@Override
public String toString() {
return "Course{" +
"cname='" + cname + '\'' +
'}';
}
```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-2.0.xsd">
<!-- 数组集合注入 -->
<bean id="stu" class="com.xuanniao.Stu">
<property name="course">
<array>
<value>spring课程</value>
<value>webapp课程</value>
</array>
</property>
<!-- list类型属性注入 -->
<property name="list">
<list>
<value>学会java</value>
<value>坚持不懈</value>
</list>
</property>
<!-- map类型属性注入 -->
<property name="map">
<map>
<entry key="人生" value="java"></entry>
<entry key="时间留给" value="Spring"></entry>
</map>
</property>
<!-- set类型属性注入 -->
<property name="StringSet">
<set>
<value>123</value>
<value>456</value>
</set>
</property>
<!-- List类型属性注入,值是对象 -->
<property name="courseList">
<list>
<ref bean="course"></ref>
<ref bean="course1"></ref>
</list>
</property>
</bean>
<!-- 创建多个course对象 -->
<bean id="course" class="spring5.collectiontype.Course">
<property name="cname" value="Spring5框架"></property>
</bean>
<bean id="course1" class="spring5.collectiontype.Course">
<property name="cname" value="Mybatis框架"></property>
</bean>
</beans>
@Test
public void test1() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
Stu stu = context.getBean("stu", Stu.class);
System.out.println(stu);
stu.test1();
}