Spring-06-DI依赖注入
构造器注入
set注入(重要)
在Spring中如何给属性(复杂的)赋值的问题
- 依赖注入:set注入
- 依赖:bean对象的创建依赖于容器
- 注入:bean对象中的所有属性,由容器来注入
模拟复杂属性
实体类
public class Student {
private String name; //普通值
private Address address;//引用类型
private String[] books;//数组
private List<String> hobbys;//列表
private Map<String,String> card;//map
private Set<String> games;//集合
private String wife;//null
private Properties info;//Properties类
...get,set,toString...
}
bean.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.cmy.pojo.Address"/>
<bean id="student" class="com.cmy.pojo.Student">
<!--1.普通值注入,直接使用value-->
<property name="name" value="cmy"/>
<!--2.引用类型注入,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>
</list>
</property>
<!--map注入-->
<property name="card">
<map>
<entry key="建设银行" value="123456"/>
<entry key="农业银行" value="654321"/>
</map>
</property>
<!--set注入-->
<property name="games">
<set>
<value>魂斗罗</value>
<value>超级玛丽</value>
<value>LOL</value>
</set>
</property>
<!--null注入-->
<!--空值符串注入和null注入不同-->
<property name="wife">
<null/>
</property>
<!--Properties注入-->
<property name="info">
<props>
<prop key="学号">11111111</prop>
<prop key="性别">男</prop>
</props>
</property>
</bean>
</beans>
测试
public class MyTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student s = (Student) context.getBean("student");
System.out.println(s);
//Student{name='cmy', address=com.cmy.pojo.Address@1d16f93d,
//books=[红楼梦, 西游记, 三国演义, 水浒传], hobbys=[看电影, 看书],
//card={建设银行=123456, 农业银行=654321}, games=[魂斗罗, 超级玛丽, LOL],
//wife='null', info={学号=11111111, 性别=男}}
}
}
其他方式注入
p命名空间注入
<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"
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"-->
<bean id="user" class="com.cmy.pojo.User" p:name="void" p:gender="男"/>
</beans>
测试
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("userbean.xml");
User user = (User) context.getBean("user");
System.out.println(user);
//User{name='void', gender='男'}
}
c命名空间注入
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<!--c命名空间注入,必须有有参构造器,通过构造器注入-->
<!--必须导入xmlns:c="http://www.springframework.org/schema/c"-->
<bean id="user2" class="com.cmy.pojo.User" c:name="xiaoming" c:gender="女"/>
</beans>