public void setDname(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return “Dept{” +
“dname='” + dname + ‘’’ +
‘}’;
}
}
Emp类:
package com.Keafmd.spring5.bean;
/**
-
Keafmd
-
@ClassName: Emp
-
@Description: 员工类
-
@author: 牛哄哄的柯南
-
@date: 2021-01-15 9:53
*/
public class Emp {
private String ename;
private String gender;
//表示员工属于某一个部门,使用对象的形式表示
private Dept dept;
public 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+" “+gender+” "+dept);
}
}
bean3.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”>
测试类:
package com.Keafmd.spring5.testdemo;
import com.Keafmd.spring5.User;
import com.Keafmd.spring5.bean.Emp;
import com.Keafmd.spring5.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
-
Keafmd
-
@ClassName: TestBean
-
@Description: 注入属性,内部bean
-
@author: 牛哄哄的柯南
-
@date: 2021-01-15 9:46
*/
public class TestBean {
@Test
public void testBean2(){
//1、载Spring的配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“bean3.xml”);
//2、获取配置文件中创建的对象 默认是执行无参的构造方法创建
Emp emp =applicationContext.getBean(“emp”, Emp.class);
//System.out.println(userservice);
emp.add();
}
}
输出结果:
小