目录
spring ioc
1 控制反转,创建对象
1.1 无参构造函数
1.2 set方式
1.3 工厂模式
2 依赖注入
2.1 手动注入
2.1.1 有参构造函数注入
2.1.2 set方法注入
2.2 自动注入
2.2.1 set 方式自动注入
2.2.2 有参构造函数自动注入
2.3 特殊构造函数注入
3 测试方法
需要用到的类对象
根据名称进行注入
根据类型进行注入
有参构造函数自动注入,需要保证有参构造函数的参数都存在,即该 xml 文件中 创建全部参数的 bean 对象
a
b
c
@Test
public void test1() {
/**
* bean-service.xml 指代的是,xml 文件
* orderManagerSet 指代的是 bean 标签中的 name 的值
*
*
*
*/
ApplicationContext ac = new ClassPathXmlApplicationContext("bean-service.xml");
OrderManagerSet orderManagerSet = (OrderManagerSet) ac.getBean("orderManagerSet");
orderManagerSet.getOrder();
}
注意测试的时候,xml 文件所在的 resource 文件夹,一定要手动的指定为 Resource Root ,否则的话,即使使用绝对路径在测试的时候,也会报 找不到 xml 文件的错误。
在写的时候,文件夹有点乱,如果直接拷贝 xml 的文件内容,需要手动的修改 class 的路径
/**
* @author yangLongFei 2020-12-24-15:19
*/
public interface OrderService {
public void getOrder();
}
/**
* @author yangLongFei 2020-12-24-15:20
*/
public class OrderServiceImpl implements OrderService {
@Override
public void getOrder() {
System.out.println("this is com.sys.yang.standard.OrderServiceImpl.getOrder ");
}
}
/**
* @author yangLongFei 2020-12-24-15:48
*/
public class OrderManagerSet {
private OrderServiceImpl orderService;
/**
* 有参构造器
*/
public OrderManagerSet(OrderServiceImpl orderService) {
System.out.println("有参构造器,com.sys.yang.standard.OrderManagerSet.OrderManagerSet(com.sys.yang.standard.OrderServiceImpl)");
this.orderService = orderService;
}
/**
* 无参构造器
*/
public OrderManagerSet() {
System.out.println("无参构造器, com.sys.yang.standard.OrderManagerSet.OrderManagerSet()");
}
/**
* set 方法
*/
public void setOrderService(OrderServiceImpl orderService) {
System.out.println("set方法 com.sys.yang.standard.OrderManagerSet.setOrderService");
this.orderService = orderService;
}
public void getOrder() {
orderService.getOrder();
System.out.println("this is com.sys.yang.standard.OrderManagerSet");
}
}
/**
* @author yangLongFei 2020-12-24-20:34
*/
public interface Father {
void getSoon();
}
/**
* @author yangLongFei 2020-12-24-20:35
*/
public class Soon1 implements Father {
@Override
public void getSoon() {
System.out.println("this is com.sys.yang.standard.factory.Soon1");
}
}
/**
* @author yangLongFei 2020-12-24-20:35
*/
public class Soon2 implements Father {
@Override
public void getSoon() {
System.out.println("this is com.sys.yang.standard.factory.Soon2 !!!!!!!!!!!!!!!!!!!");
}
}
/**
* @author yangLongFei 2020-12-24-20:36
*/
public class FactherFactory {
public Father getSoon1() {
return new Soon1();
}
public Father getSoon2() {
return new Soon2();
}
}
/**
* @author yangLongFei 2020-12-24-16:52
*/
public class Score {
private String type;
private char level;
public Score() {
}
public Score(String type, char level) {
this.type = type;
this.level = level;
}
@Override
public String toString() {
return "Score{" +
"type='" + type + '\'' +
", level=" + level +
'}';
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getLevel() {
return level;
}
public void setLevel(char level) {
this.level = level;
}
}
/**
* @author yangLongFei 2020-12-24-16:10
*/
public class Student {
private int id;
private String name;
private String age;
private Score score;
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age='" + age + '\'' +
", score=" + score +
'}';
}
public Student() {
System.out.println("student 无参构造函数 com.sys.yang.standard2.Student.Student()");
}
public Student(int id, String name, String age) {
System.out.println("student 有参构造函数 com.sys.yang.standard2.Student.Student(int, java.lang.String, java.lang.String)");
this.id = id;
this.name = name;
this.age = age;
}
public Student(int id, String name, String age, Score score) {
this.id = id;
this.name = name;
this.age = age;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Score getScore() {
return score;
}
public void setScore(Score score) {
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
import java.util.Arrays;
import java.util.List;
/**
* @author yangLongFei 2020-12-24-19:22
*/
public class StudentDto {
private Student student;
private List scoreList;
private String[] stringArray;
public StudentDto(Student student, List scoreList, String[] stringArray) {
this.student = student;
this.scoreList = scoreList;
this.stringArray = stringArray;
}
public StudentDto() {
}
@Override
public String toString() {
return "StudentDto{" +
"student=" + student +
", scoreList=" + scoreList +
", stringArray=" + Arrays.toString(stringArray) +
'}';
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public List getScoreList() {
return scoreList;
}
public void setScoreList(List scoreList) {
this.scoreList = scoreList;
}
public String[] getStringArray() {
return stringArray;
}
public void setStringArray(String[] stringArray) {
this.stringArray = stringArray;
}
}