1. 构造器注入(前边已讲)
2. Set方式注入【重点】
2.1 依赖注入含义
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象的所有属性,由容器来注入
2.2 环境搭建
- 复杂类型
创建引用类型Address
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 + '\'' +
'}';
}
}
- 真实测试对象
创建类Student
package com.kuang.pojo;
import lombok.Data;
import java.util.*;
@Data
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;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
- 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">
<bean id="student" class="com.kuang.pojo.Student">
<property name="name" value="星时"></property>
</bean>
</beans>
- 测试类
public class MyTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
}
}
2.3 完善注入
- 注入
<?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="address" class="com.kuang.pojo.Address">
<property name="address" value="东胜神洲花果山水帘洞"></property>
</bean>
<bean id="student" class="com.kuang.pojo.Student">
<property name="name" value="星时"></property>
<property name="address" ref="address"></property>
<property name="books">
<array>
<value>杀死一只知更鸟</value>
<value>醒来觉得甚是爱你</value>
<value>明朝那些事儿</value>
</array>
</property>
<property name="hobbies">
<list>
<value>象棋</value>
<value>羽毛球</value>
<value>跑步</value>
</list>
</property>
<property name="card">
<map>
<entry key="身份证" value="2394872394382472935482"></entry>
<entry key="银行卡" value="4357293854729384752398"></entry>
<entry key="饭卡" value="294723945735732854548557"></entry>
</map>
</property>
<property name="games">
<set>
<value>王者荣耀</value>
<value>和平精英</value>
<value>红色警戒</value>
</set>
</property>
<property name="wife">
<null/>
</property>
<property name="info">
<props>
<prop key="姓名">星时</prop>
<prop key="年龄">232</prop>
<prop key="智商">250</prop>
<prop key="email">23424729@qq.com</prop>
</props>
</property>
</bean>
</beans>
- 测试
public class MyTest {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
3. 拓展方式注入
3.1 p命名空间注入——主要针对无参构造方法
- User类
@Data
public class User {
private String name;
private int age;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
- userBean.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.kuang.pojo.User" p:name="阿飞" p:age="14"/>
</beans>
- 测试方法
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("userBean.xml");
User user = context.getBean("user", User.class);
System.out.println(user.toString());
}
<!--
运行结果:
User{name='阿飞', age=14}
-->
3.2 c命名空间注入——主要针对有参构造方法
- User类(相比上边多了两个构造方法)
@Data
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
- userBean.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.kuang.pojo.User" p:name="阿飞" p:age="14"/>
<bean id="user2" class="com.kuang.pojo.User" c:name="星时" c:age="123"/>
</beans>
- 测试方法
@Test
public void test(){
ApplicationContext context=new ClassPathXmlApplicationContext("userBean.xml");
User user = context.getBean("user2", User.class);
System.out.println(user.toString());
}
<!--
运行结果:
User{name='星时', age=123}
-->
注意:使用p命名空间和c命名空间需要导入约束