<?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">
<bean id="pojo" class="di.Pojo"/>
<!-- setter注入,必须提供对应属性的set方法-->
<bean id="d1" class="di.DiTest">
<property name="age" value="18"/>
<property name="name" value="mika"/>
<property name="pojo" ref="pojo"/>
<property name="list">
<list value-type="java.lang.Integer">
<value>1</value>
<value>2</value>
<value>3</value>
</list>
</property>
<property name="map">
<map key-type="java.lang.Integer" value-type="java.lang.String">
<entry key="1" value="a"/>
<entry key="2" value="b"/>
<entry key="3" value="c"/>
</map>
</property>
</bean>
<!--无参构造方法注入-->
<bean id="d2" class="di.DiTest"/>
<!--有参构造方法注入,可以使用参数名称或使用参数下标进行对应,或混用,参数个数对应不同有参构造,相同个数的构造根据参数类型进行对应 -->
<bean id="d3" class="di.DiTest">
<!-- type默认为String,建议指明类型 -->
<constructor-arg name="age" type="int" value="18"/>
<constructor-arg name="name" type="java.lang.String" value="mika"/>
<constructor-arg index="2" type="di.Pojo" ref="pojo"/>
</bean>
</beans>
package di;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author LiFeng
* @create 2021-03-09 10:13
* @describe
*/
public class DiTest {
private int age;
private String name;
private Pojo pojo;
private List<Integer> list = new ArrayList<>();
private Map<Integer, String> map = new HashMap<>();
DiTest() {}
DiTest(int age) {
this.age = age;
}
DiTest(int age, String name) {
this.age = age;
this.name = name;
}
public DiTest(int age, String name, Pojo pojo) {
this.age = age;
this.name = name;
this.pojo = pojo;
}
@Override
public String toString() {
return "DiTest{" +
"age=" + age +
", name='" + name + '\'' +
", pojo=" + pojo +
", list=" + list +
", map=" + map +
'}';
}
public List<Integer> getList() {
return list;
}
public void setList(List<Integer> list) {
this.list = list;
}
public Map<Integer, String> getMap() {
return map;
}
public void setMap(Map<Integer, String> map) {
this.map = map;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Pojo getPojo() {
return pojo;
}
public void setPojo(Pojo pojo) {
this.pojo = pojo;
}
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
DiTest di1 = (DiTest)applicationContext.getBean("d1");
System.out.println(di1);
DiTest di2 = (DiTest)applicationContext.getBean("d2");
System.out.println(di2);
DiTest di3 = (DiTest)applicationContext.getBean("d3");
System.out.println(di3);
}
}
package di;
/**
* @author LiFeng
* @create 2021-03-09 16:00
* @describe
*/
public class Pojo {
}
结果如下
DiTest{age=18, name='mika', pojo=di.Pojo@56aac163, list=[1, 2, 3], map={1=a, 2=b, 3=c}}
DiTest{age=0, name='null', pojo=null, list=[], map={}}
DiTest{age=18, name='mika', pojo=di.Pojo@56aac163, list=[], map={}}
其中关于p命名空间注入以及更多细节,参考博文
https://www.cnblogs.com/nhdlb/p/12426941.html
IOC的好处思考,参考文章
https://www.zhihu.com/question/23277575