Spring其他类型的注入
【摘要】
Bean 标签下的 <property> 元素中,使用以下元素配置 Java 集合类型的属性和参数,例如 List、Set、Map 以及 Properties 等
OtherType ...
Bean 标签下的 <property>
元素中,使用以下元素配置 Java 集合类型的属性和参数,例如 List、Set、Map 以及 Properties 等
OtherType 类
public class OtherType {
/*数组类型*/
private String[] arr;
/*list集合类型*/
private List<String> list;
/*map集合类型*/
private Map<String,String> map;
/*set集合类型*/
private Set<String> set;
public void setArr(String[] arr) {
this.arr = arr;
}
public void setList(List<String> list) {
this.list = list;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
public void setSet(Set<String> set) {
this.set = set;
}
@Override
public String toString() {
return "OtherType{" +
"arr=" + Arrays.toString(arr) +
", list=" + list +
", map=" + map +
", set=" + set +
'}';
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
spring.xml
<bean id="otherType" class="com.liu.c.OtherType">
<!--数组-->
<property name="arr">
<array>
<value>Java</value>
<value>PHP</value>
<value>C语言</value>
</array>
</property>
<!--list-->
<property name="list">
<list>
<value>小张</value>
<value>小刘</value>
</list>
</property>
<!--map-->
<property name="map">
<map>
<entry key="Java" value="java"></entry>
<entry key="PHP" value="php"></entry>
</map>
</property>
<!--set-->
<property name="set">
<set>
<value>MySQL</value>
<value>Redis</value>
</set>
</property>
</bean>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
Test测试
public class TestOtherType {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
OtherType otherType = context.getBean("otherType", OtherType.class);
System.out.println(otherType);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
结果,成功注入
OtherType{
arr=[Java, PHP, C语言],
list=[小张, 小刘],
map={Java=java, PHP=php},
set=[MySQL, Redis]}
- 1
- 2
- 3
- 4
- 5
如果要注入的属性是对象类型的,使用 ref 即可
文章来源: blog.csdn.net,作者:周棋洛ყ ᥱ ᥉,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/m0_53321320/article/details/123571819
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)