DI [Dependency Injection]
依赖注入:两种注入方式(属性注入和构造注入)属性注入(即setter方法注入)构造方法注入
依赖注入:
依赖:指Bean对象的创建依赖于容器、注入:指Bean对象所依赖的资源由容器来设置和装配
两种注入方式(属性注入和构造注入)
属性注入(即setter方法注入)
编写实体类:
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
public class Student {
private String name;
private Address address;
private String[] books;
private List
<String> hobbys;
private Map
<String,String> card;
private Set
<String> games;
private String wife;
private Properties info;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setAddress(Address address) {
this.address = address;
}
public void setBooks(String[] books) {
this.books = books;
}
public void setHobbys(List
<String> hobbys) {
this.hobbys = hobbys;
}
public void setCard(Map
<String, String> card) {
this.card = card;
}
public void setGames(Set
<String> games) {
this.games = games;
}
public void setWife(String wife) {
this.wife = wife;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
在 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
.xsd"
>
<!-- <bean id
="student" class="com.luming.pojo.Student">-->
<!-- <property name
="name" value
="鹿鸣"></property
>-->
<!-- </bean
>-->
<bean id
="addressi" class="com.luming.pojo.Address">
<property name
="address" value
="陕西西安"></property
>
</bean
>
<bean id
="student" class="com.luming.pojo.Student">
<property name
="name" value
="小米"></property
>
<property name
="address" ref
="addressi"></property
>
<property name
="books">
<array>
<value>西游记
</value
>
<value>简爱
</value
>
</array
>
</property
>
<property name
="hobbys">
<list>
<value>写代码
</value
>
<value>唱歌
</value
>
</list
>
</property
>
<property name
="card">
<map>
<entry key
="建行" value
="12345678955636363"></entry
>
</map
>
</property
>
<property name
="games">
<set>
<value>王者
</value
>
<value>吃鸡
</value
>
</set
>
</property
>
<property name
="wife">
<null></null
>
</property
>
<property name
="info">
<props>
<prop key
="学号">41709050885</prop
>
<prop key
="班级">软二
</prop
>
</props
>
</property
>
</bean
>
</beans
>
构造方法注入
基于构造器的 DI 通过调用带参数的构造方法实现,每个参数代表一个依赖。
编写实体类:
public class User {
private String name;
private int age;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
}
在 applicationContext.xml 中添加配置信息
<bean id="user" class="com.luming.pojo.User">
<constructor-arg index="0" value="鹿鸣">
</constructor-arg>
<constructor-arg index="1" value="20">
</constructor-arg>
</bean>
测试:
@Test
public void test1() {
ApplicationContext context
=new ClassPathXmlApplicationContext("applicationContext.xml");
User u
= (User
)context
.getBean("user");
System
.out
.println(u
);
}