1、构造器注入
前面已经讲了(去看前面的笔记)
2、set方式注入(重点)
依赖:bean对象的创建依赖于容器
注入:bean对象中的所有属性,由容器来注入
搭建测试环境
Spring配置文件
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
创建测试类
package com.li.pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
package com.li.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{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
构造器注入测试环境是否正确
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.li.pojo.Student">
<property name="name" value="张三"/>
</bean>
</beans>
package com.li.pojo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestGo {
@Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
各种DI注入
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 普通注入-->
<bean id="add" class="com.li.pojo.Address">
<property name="address" value="武汉"/>
</bean>
<bean id="student" class="com.li.pojo.Student">
<!--普通注入-->
<property name="name" value="张三"/>
<!--Bean值注入
ref=接需要注入的bean的id
-->
<property name="address" ref="add"/>
<!--数组注入
-->
<property name="books">
<array>
<value>语文</value>
<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>
</list>
</property>
<!--Map注入-->
<property name="card">
<map>
<entry key="身份证" value="213456154236458454"/>
<entry key="银行卡" value="1254147282525"/>
</map>
</property>
<!--Set注入-->
<property name="games">
<set>
<value>cs</value>
<value>cf</value>
<value>lol</value>
</set>
</property>
<!--空值注入
<property name="wife" value=""/>
-->
<!--null值注入-->
<property name="wife">
<null/>
</property>
<!--properties-->
<property name="info">
<props>
<prop key="utl">jabc</prop>
<prop key="UserName">root</prop>
</props>
</property>
</bean>
</beans>
配置文件实例
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
P命名空间注入
需要加配置文件
xmlns:p="http://www.springframework.org/schema/p"
<!--P命名空间注入可以直接注入属性值-->
<bean id="user1" class="com.li.pojo.User" p:name="张三" p:age="23"/>
C命名空间注入
需要加配置文件
xmlns:c="http://www.springframework.org/schema/c"
<!--C命名空间,通过构造器注入-->
<bean id="user2" class="com.li.pojo.User" c:name="李四" c:age="33"/>