本文共 4953 字,大约阅读时间需要 16 分钟。
------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥-------------
DI和IOC相比,DI更偏向于实现
DI的set方式注入在前面入门案例里有写,所以此处不多啰嗦,直接开搞,先说构造注入和P命名注入
构造方式,理所当然要有带参构造,这儿值得注意的是,你最好再补全一个无参构造,因为你写了带参构造,系统就不再会为你默认补全一个无参构造了,当你在不经意或者不知情的情况下被调用了,就会报错
P命名则有注意的是那个头文件 xmlns xsi需要你去配置一道,我下面有,你直接copy就可以
在实体类中(有俩个实体类,我做了关联关系)
package cn.dawn.day05diup;/** * Created by Dawn on 2018/3/5. */public class Car { private String color; private String type; public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getType() { return type; } public void setType(String type) { this.type = type; }}package cn.dawn.day05diup;/** * Created by Dawn on 2018/3/5. *///student类public class Student { private String name; private Integer age; private Car car; //带参构造 public Student(String name, Integer age, Car car) { this.name = name; this.age = age; this.car = car; } //无参构造 public Student() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; }}
在大配置xml文件中
没有什么好讲的,带参构造的注入方法,index索引从0开始,对应的是那个带参构造的index
单测方法:
@Test /*diP命名注入*/ public void t02(){ ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml"); Student student = (Student) context.getBean("student"); System.out.println("学生"+student.getName()+"开着"+student.getCar().getColor()+"的"+student.getCar().getType()); } @Test /*di构造注入*/ public void t01(){ ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml"); Student student = (Student) context.getBean("student"); System.out.println("学生"+student.getName()+"开着"+student.getCar().getColor()+"的"+student.getCar().getType()); }
集合注入:
数组,List,Set,Map,Properties
实体类
package cn.dawn.day05diup;import java.util.*;/** * Created by Dawn on 2018/3/5. */public class MyCollection { private String[] array; private Listlist; private Set set; private Map map; private Properties properties; @Override public String toString() { return "MyCollection{ " + "array=" + Arrays.toString(array) + ", list=" + list + ", set=" + set + ", map=" + map + ", properties=" + properties + '}'; } public String[] getArray() { return array; } public void setArray(String[] array) { this.array = array; } public List getList() { return list; } public void setList(List list) { this.list = list; } public Set getSet() { return set; } public void setSet(Set set) { this.set = set; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } public Properties getProperties() { return properties; } public void setProperties(Properties properties) { this.properties = properties; }}
大配置中的bean节点
孟六 孟六十六 孟六百六十六
奥迪 奥小迪 奥迪迪 set1 set2 set3 v1 v2 v3
单测
@Test /*di集合注入*/ public void t03(){ ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext-day05diup.xml"); MyCollection mycollection = (MyCollection) context.getBean("mycollection"); System.out.println(mycollection); }
由于重写了toString,可以直接一览无余
转载地址:http://evjil.baihongyu.com/