Spring IOC 依赖注入
-
依赖:bean对象的创建依赖于容器
-
注入:bean对象中的所有属性,由容器来注入
实体类:
package com.xia.pojo; import lombok.Data; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; @Data public class Student { private String type; private String name; private Address address; private String[] books; private List<String> hobby; private Map<String,String> card; private Set<String> game; private String wife; private Properties info; public Student(String type) { this.type = type; } }
总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"> <import resource="Bean.xml"></import> </beans>
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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="address" class="com.xia.pojo.Address"> <property name="address" value="山东"></property> </bean> <bean id="student" class="com.xia.pojo.Student"> <!-- 构造方法注入--> <constructor-arg index="0" value="Sping"></constructor-arg> <!-- 1.普通值注入,使用value--> <property name="name" value="夏小皮"></property> <!-- 2.Bean注入--> <property name="address" ref="address"></property> <!-- 3.数组注入--> <property name="books"> <array> <value>红楼梦</value> <value>西游记</value> <value>三国演义</value> </array> </property> <!-- 4.List注入--> <property name="hobby"> <list> <value>听歌</value> <value>代码</value> <value>电影</value> </list> </property> <!-- 5.map注入--> <property name="card"> <map> <entry key="银行卡" value="454545"></entry> <entry key="身份证" value="454545454787"></entry> </map> </property> <!-- 6.set注入--> <property name="game"> <set> <value>LOL</value> </set> </property> <!-- 7.空值注入 --> <property name="info"> <null></null> </property> </bean> </beans>