Spring XML配置<Map>注入Bean属性举例
来源:互联网 发布:如何更改手机淘宝密码 编辑:程序博客网 时间:2024/05/21 10:21
Spring <Map>元素用来存储多个键值对属性,类型为java.util.Map;他的子标签<entry>用来定义Map中的键值实体,下面举例说明;
Article.java
这个article class有一个属性是作者联名信息,使用序号和作者名来构成一个Map属性.
import java.util.*;
public class Article
{
private String title;
private Map<String, String> authorsInfo;
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public void setAuthorsInfo(Map<String, String> authorsInfo) {
this.authorsInfo = authorsInfo;
}
public Map<String, String> getAuthorsInfo() {
return authorsInfo;
}
}
spring-beans.xml:
<map>元素用于提供具体的实体键值配置,通过<entry>将序号和作者名称进行绑定注入。
<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-2.5.xsd">
<bean id="article" class="Article">
<property name="title" value="RoseIndia"/>
<property name="authorsInfo">
<map>
<entry key="1" value="Deepak" />
<entry key="2" value="Arun"/>
<entry key="3" value="Vijay" />
</map>
</property>
</bean>
</beans>
RunDemoMain.java
测试主程代码;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.*;
public class AppMain
{
public static void main( String[] args )
{
ApplicationContext appContext =
new ClassPathXmlApplicationContext(new String[] {"spring-beans.xml"});
Article article = (Article)appContext.getBean("article");
System.out.println("Article Title: "+article.getTitle());
Map<String, String> authorsInfo = article.getAuthorsInfo();
System.out.println("Article Author: ");
for (String key : authorsInfo.keySet()) {
System.out.println(key + " : "+(String)authorsInfo.get(key));
}
}
}
输出结果如下:
Article Title: RoseIndia
Article Author:
1 : Deepak
2 : Arun
3 : Vijay
转自:http://www.txdnet.cn/log/20221059000001.xhtm- Spring XML配置<Map>注入Bean属性举例
- spring的注入参数(bean对象中又有bean属性),beans.xml配置
- [Spring] 注入Bean属性
- Spring xml配置注入数组/list/Map
- Spring的《XML显式装配bean》- 注入Map
- 3.Spring bean常见属性的注入:applicationContext-beans.xml
- 4.Spring bean常见属性的注入:applicationContext-editor.xml
- spring 3 bean配置--注入属性配置细节(1)
- spring 4 bean配置--注入属性配置细节(2)
- Spring boot将配置属性注入到bean类中
- Spring boot将配置属性注入到bean类中
- spring bean setter属性注入
- spring bean setter属性注入
- spring中bean的高级属性之list, set, map以及props元素(含举例)
- spring中bean的高级属性之list, set, map以及props元素(含举例)
- spring中bean的高级属性之list, set, map以及props元素(含举例)
- Spring的《XML显式装配bean》之通过属性property注入bean
- Spring配置bean属性
- MCISendString详细参数
- TCP/IP HTTP Socket基本知识
- 关于java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required 类似问题的解决总结
- mysql基本操作
- [EECS申请与就业专辑]之EE/ECE各个方向就业浅谈
- Spring XML配置<Map>注入Bean属性举例
- 【设计模式攻略】行为型模式之Mediator模式
- Java正则表达式(三)、代码量统计工具(统计java源文件中注释、代码、空白行数量)
- Oracle 10g 数据库IMP导入问题
- 足球 踩单车
- 7.OD-字符串参考
- vim---移动操作
- Windows下SVN Server搭建
- 稀疏矩阵的压缩存储及其转置算法