1 构造器注入
constructor-arg,前面已经论述
2 Set方式注入[重点]
-
依赖注入:Set注入
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,由容器来注入
[环境搭建]
2.1 复杂类型
package com.kwok.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 + '\'' +
'}';
}
}
2.2 真实测试对象
package com.kwok.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.toString() +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
2.3 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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.kwok.pojo.Address">
<property name="address" value="上海"/>
</bean>
<bean id="student" class="com.kwok.pojo.Student">
<!--第一种,普通值注入,value-->
<property name="name" value="剑圣"/>
<!--第二种,Bean注入,ref-->
<property name="address" ref="address"/>
<!--第三种,数组注入-->
<property name="books">
<array>
<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="520"/>
<entry key="银行卡" value="88888888"/>
</map>
</property>
<!--第六种,Set注入-->
<property name="games">
<set>
<value>LOL</value>
<value>ES5</value>
</set>
</property>
<!--第七种,null注入-->
<property name="wife">
<null/>
</property>
<!--第八种,Properties注入-->
<property name="info">
<props>
<prop key="学号">20190525</prop>
<prop key="性别">男</prop>
<prop key="姓名">小明</prop>
</props>
</property>
</bean>
</beans>
2.4 测试类
@Test
public void getStudent(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
/*
Student{
name='剑圣',
address=Address{address='上海'},
books=[红楼梦, 西游记, 水浒传, 三国演义],
hobbys=[听歌, 敲代码, 看电影],
card={身份证=520, 银行卡=88888888},
games=[LOL, ES5],
wife='null',
info={姓名=小明, 学号=20190525, 性别=男}
}
*/
}
3 拓展方式注入
我们可以使用p命名空间和c命名空间进行注入
p命名:取代property
c命名:取代constructor-arg
<?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命名空间注入,可以直接注入属性的值-->
<bean id="user" class="com.kwok.pojo.User" p:name="翠花" p:age="18"/>
<!--c命名空间注入,通过构造器注入:construct-args-->
<bean id="user2" class="com.kwok.pojo.User" c:name="二狗" c:age="19"/>
</beans>
测试:
@Test
public void getUser(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = (User) context.getBean("user");
User user2 = (User) context.getBean("user2");
System.out.println(user.toString());
System.out.println(user2.toString());
}
注意点:
p命名和c命名空间不能直接使用,需要导入xml约束!
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
4 Bean的作用域
4.1 单例模式(Spring默认机制)
bean一旦设置,所有取出的类都是同一个
<bean id="user2" class="com.kwok.pojo.User" c:name="二狗" c:age="19" scope="singleton"/>
4.2 原型模式:每次从容器中get的时候,都会产生一个新对象!
<bean id="user2" class="com.kwok.pojo.User" c:name="二狗" c:age="19" scope="prototype"/>