依赖注入
- 依赖注入(Dependency Injection,DI)。
- 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
- 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 .
1 构造器注入
上一篇的
2 set注入 (重点)
要求被注入的属性 , 必须有set方法 , set方法的方法名由set + 属性首字母大写 , 如果属性是boolean类型, 没有set方法 , 是 is .
创建实体类:
package com.lx.pojo;
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 + '\'' +
'}';
}
}
package com.lx.pojo;
import java.util.*;
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 Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String[] getBooks() {
return books;
}
public void setBooks(String[] books) {
this.books = books;
}
public List<String> getHobbys() {
return hobbys;
}
public void setHobbys(List<String> hobbys) {
this.hobbys = hobbys;
}
public Map<String, String> getCard() {
return card;
}
public void setCard(Map<String, String> card) {
this.card = card;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" + '\n'+
"name='" + name + '\'' +'\n'+
address+'\n'+
"books=" + Arrays.toString(books) +'\n'+
"hobbys=" + hobbys +'\n'+
"card=" + card +'\n'+
"games=" + games +'\n'+
"wife='" + wife + '\'' +'\n'+
"info=" + info +'\n'+
'}';
}
}
包含了所有的注入方式: 这里Student通过无参构造,无参不写默认创建的
在beans.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">
<!-- 对Address类注册对象-->
<bean id="address" class="com.lx.pojo.Address">
<property name="address" value="希望以后可以去天津"/>
</bean>
<!--对Student类注册对象 无参创建,通过property注入属性-->
<bean id="student" class="com.lx.pojo.Student">
<property name="name" value="lx"/>
<!-- bean注入 对象ref方式引入-->
<property name="address" ref="address"/>
<!-- 数组注入-->
<property name="books" >
<array>
<value>语文</value>
<value>数学</value>
<value>英语</value>
<value>物理</value>
<value>化学</value>
<value>生物</value>
</array>
</property>
<!-- List注入-->
<property name="hobbys">
<list>
<value>女</value>
<value>搬砖</value>
<value>电影</value>
<value>唱歌</value>
</list>
</property>
<!-- Map注入-->
<property name="card">
<map>
<entry key="身份证" value="612516155812799264"/>
<entry key="中行卡" value="546213254665413545"/>
</map>
</property>
<!-- set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>王者荣耀</value>
<value>pubg</value>
</set>
</property>
<!-- null注入-->
<property name="wife">
<null/>
</property>
<!-- Properties-->
<property name="info">
<props>
<prop key="学号" >1008039</prop>
<prop key="姓名">lx</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
测试单元:
@Test
public void testStudent(){
//set注入集合测试
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
测试结果:
p / c 注入
引入条件
xmlns:p=“http://www.springframework.org/schema/p”
xmlns:c=“http://www.springframework.org/schema/c”
其实就是无参构造propetry 和有参构造的 construct-args 的简写
感觉没什么可说的 过
package com.lx.pojo;
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
<!--p命名空间注入,可以直接注入属性的值:property-->
<bean id="user" class="com.lx.pojo.User" p:age="18" p:name="lx" />
<!--c命名空间注入,需要有参构造器,通过构造器注入:construct-args-->
<bean id="user2" class="com.lx.pojo.User" c:age="20" c:name="wxy" />
@Test
public void testPC(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User userP = context.getBean("userP", User.class);
System.out.println(userP.toString());
System.out.println("===========");
User userC = context.getBean("userC", User.class);
System.out.println(userC.toString());
}