Spring依赖注入的方式有两种,分别是构造器依赖注入和set方法依赖注入。
第一种方式(构造器):
1.新建Student.java
public class Student {
public void say(){
System.out.println("say");
}
}
2.新建Person.java
public class Person {
private Long pid;
private String pname;
private Student student;
public Person(Long pid,String pname){
this.pid = pid;
this.pname = pname;
}
public Person(String pname,Student student){
this.pname = pname;
this.student = student;
}
public Long getPid() {
return pid;
}
public String getPname() {
return pname;
}
public Student getStudent() {
return student;
}
}
3.配置applicationContext.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.5.xsd">
<bean id="person" class="spring.di.xml.constructor.Person">
<!--
构造函数的参数
index:第几个参数,下标从0开始
type:参数的类型
ref :如果类型是引用类型,赋值
value:如果类型是基本类型,赋值
说明:
只能指定一个构造函数
-->
<constructor-arg index="0" type="java.lang.Long" value="5"></constructor-arg>
<constructor-arg index="1" type="java.lang.String" value="张三"></constructor-arg>
<!-- <constructor-arg index="2" type="spring.di.xml.constructor.Student" ref="student"></constructor-arg> -->
</bean>
<bean id="student" class="spring.di.xml.constructor.Student"></bean>
</beans>
4.新建工具类SpringHelper.java
public class SpringHelper {
public static ApplicationContext context;
public static String path;
@Before
public void startSpring(){
context = new ClassPathXmlApplicationContext(path);
}
}
5.新建测试类PersonTest.java
public class PersonTest extends SpringHelper{
static{
path = "spring/di/xml/constructor/applicationContext.xml";
}
@Test
public void test(){
Person person = (Person) context.getBean("person");
System.out.println(person.getPid());
System.out.println(person.getPname());
}
}
第二种方式(set方法):
1.新建Student.java
public class Student {
public void say(){
System.out.println("say");
}
}
2.新建Person.java
public class Person {
private Long pid;//包装类型
private String pname;//String类型
private Student student;//引用类型
private List lists;
private Set sets;
private Map map;
private Properties properties;
public Long getPid() {
return pid;
}
public void setPid(Long pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
public Set getSets() {
return sets;
}
public void setSets(Set sets) {
this.sets = sets;
}
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;
}
}
3.配置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.5.xsd">
<bean id="person" class="spring.di.xml.set.Person">
<!--
property:属性(name是根据set方法的)
在spring中
基本类型(包装类型和String类型)都可以用value来赋值
引用类型用ref赋值
-->
<property name="pid" value="5"></property>
<property name="pname" value="张三"></property>
<property name="student">
<ref bean="student"/>
</property>
<property name="lists">
<list>
<value>list1</value>
<value>list2</value>
<ref bean="student"/>
</list>
</property>
<property name="sets">
<set>
<value>set1</value>
<value>set2</value>
<ref bean="student"/>
</set>
</property>
<property name="map">
<map>
<entry key="map1">
<value>map1</value>
</entry>
<entry key="map2">
<value>map2</value>
</entry>
<entry key="map3">
<ref bean="student"/>
</entry>
</map>
</property>
<property name="properties">
<props>
<prop key="prop1">
prop1
</prop>
</props>
</property>
</bean>
<bean id="student" class="spring.di.xml.set.Student"></bean>
</beans>
4.新建测试类
public class PersonTest extends SpringHelper{
static{
path = "spring/di/xml/set/applicationContext.xml";
}
@Test
public void test(){
Person person = (Person) context.getBean("person");
person.getStudent().say();
System.out.println(person.getPid());
System.out.println(person.getPname());
List lists = person.getLists();
for(int i = 0;i < lists.size();i++){
System.out.println(lists.get(i).toString());
}
}
}