有三种 1 设值注入 2 构造注入 3 实现特定接口注入(因为要实现特定接口 是侵入式编程 污染代码 基本不用)
设值注入:通过setter方法注入 value 基本数据类型和字符串 ref 传引用
<?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">
<!-- 注册School-->
<bean id="mySchool" class="com.gqc.di01.School">
<property name="name" value="清华大学" />
</bean>
<!-- 注册Student-->
<bean id="myStudent" class="com.gqc.di01.Student">
<property name="name" value="张三"/>
<property name="age" value="11" />
<property name="school" ref="mySchool"/>
</bean>
</beans>
package com.gqc.di01;
public class Student {
private String name;
private int age;
private School school;//对象属性 域属性
public void setName(String name) {
System.out.println("执行setName()");
this.name = name;
}
public void setAge(int age) {
System.out.println("执行setAge()");
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", school=" + school
+ "]";
}
public void setSchool(School school) {
this.school = school;
}
}
package com.gqc.di01;
public class School {
private String name;
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "School [name=" + name + "]";
}
}
package com.gqc.di01;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test01() {
//创建容器对象 加载Spring 配置文件
String resource = "com/gqc/di01/appliactionContext.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(resource);
Student student=(Student) ac.getBean("myStudent");
System.out.println(student);
}
}
构造注入 通过带参构造器注入 (用的较少 因为参数可能太多)
<?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">
<!-- 注册School-->
<bean id="mySchool" class="com.gqc.di02.School">
<property name="name" value="清华大学" />
</bean>
<!-- 注册Student-->
<bean id="myStudent" class="com.gqc.di02.Student">
<constructor-arg name="name" value="李四"/>
<constructor-arg name="age" value="24"/>
<constructor-arg name="school" ref="mySchool" />
<!-- <constructor-arg index="0" value="李四"/>
<constructor-arg index="1" value="24"/>
<constructor-arg index="2" ref="mySchool" />
-->
<!-- <constructor-arg value="李四"/>
<constructor-arg value="24"/>
<constructor-arg ref="mySchool" />
-->
</bean>
</beans>
package com.gqc.di02;
public class Student {
private String name;
private int age;
private School school;//
public Student(String name, int age, School school) {
super();
this.name = name;
this.age = age;
this.school = school;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", school=" + school
+ "]";
}
}
package com.gqc.di02;
public class School {
private String name;
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "School [name=" + name + "]";
}
}
package com.gqc.di02;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
@Test
public void test01() {
//创建容器对象 加载Spring 配置文件
String resource = "com/gqc/di02/appliactionContext.xml";
ApplicationContext ac=new ClassPathXmlApplicationContext(resource);
Student student=(Student) ac.getBean("myStudent");
System.out.println(student);
}
}