spring中有不同类型的注入的方式
1:实体类
address
public class Address {
private String address;
}
Student
public class Student {
//姓名
private String name;
//家庭住址
private Address address;
//喜欢的书
private String[] books;
//爱好
private List<String> hobby;
//卡号
private Map<String,String> card;
//喜欢的游戏
private Set<String> games;
// 妻子
private String wife;
//属性
private Properties info;
}
在resource文件下面建一个xxx.xml配置文件
然后在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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="address" class="com.test.Address">
<property name="address" value="深圳"/>
</bean>
<bean id="student" class="com.test.Student">
</bean>
</beans>
然后在**id=“student”**的这个bean下进行各种方式的注入
<!-- 普通注入-->
<property name="name" value="阿杰"/>
<!-- bean注入,引用上面定义的bean-->
<property name="address" ref="address"/>
<!-- 数组-->
<property name="books">
<array>
<value>万族之劫</value>
<value>魔临</value>
<value>雪中悍刀行</value>
</array>
</property>
<!-- List-->
<property name="hobby">
<list>
<value>听歌</value>
<value>打王者</value>
<value>看小说</value>
</list>
</property>
<!-- Map-->
<property name="card">
<map>
<entry key="身份证号" value="123456789123456"/>
<entry key="银行卡号" value="123456789123456"/>
</map>
</property>
<!-- Set-->
<property name="games">
<set>
<value>LOL</value>
<value>COC</value>
<value>BOB</value>
</set>
</property>
<!-- null -->
<property name="wife">
<null/>
</property>
<!-- Properties-->
<property name="info">
<props>
<prop key="学号">1811030029</prop>
<prop key="性别">男</prop>
</props>
</property>
结果:
Student{
name=‘阿杰’,
address=Address{address=‘深圳’},
books=[万族之劫, 魔临, 雪中悍刀行],
hobby=[听歌, 打王者, 看小说],
card={身份证号=123456789123456,
银行卡号=123456789123456},
games=[LOL, COC, BOB],
wife=‘null’,
info={学号=1811030029, 性别=男}
}