`
v5browser
  • 浏览: 1130474 次
社区版块
存档分类
最新评论

Spring中Bean属性为集合的注入方式

 
阅读更多

实体类CollectionBean{List<String> cityList,Set<String> citySet,String[] cityArray,Map<Integer, String> mms,Properties props} get(),set()方法

applicationContext.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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
			http://www.springframework.org/schema/beans 
			http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
			http://www.springframework.org/schema/aop 
			http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
			http://www.springframework.org/schema/tx 
			http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- new CollectionBean -->
<bean id="collectionBean" class="com.tarena.entity.CollectionBean">
  <!-- list 注入 -->
  <property name="cityList">
  	<list>
  		<value>北京</value>
  		<value>上海</value>
  		<value>北京</value>
  		<value>广州</value>
  	</list>
  </property>
  <!-- set注入 -->
  <property name="citySet">
  	<set>
  		<value>北京</value>
  		<value>上海</value>
  		<value>北京</value>
  		<value>广州</value>
  	</set>
  </property>
  <!-- 数组 注入 -->
  <property name="cityArray">
  	<list>
  		<value>北京</value>
  		<value>上海</value>
  		<value>广州</value>
  		<value>北京</value>
  	</list>
  </property>
  <!-- Map注入 -->
  <property name="mms">
  	<map>
  		<entry key="1" value="如花"/>
  		<entry key="2" value="如来"/>
  		<entry key="3" value="如草"/>
  	</map>
  </property>
  <!-- Properties key=value -->
  <property name="props">
  	<props>
  		<prop key="driverClassName">
  		   com.mysql.jdbc.Driver
  		</prop>
  		<prop key="url">
  			jdbc:mysql://127.0.0.1:3306/mydb
  		</prop>
  		<prop key="username">root</prop>
  		<prop key="password">1234</prop>
  	</props>
  </property>
</bean>

</beans>


测试类:

public class CollectionBeanTest {
	private static Log log = LogFactory.getLog(CollectionBeanTest.class);
	CollectionBean collectionBean = null;
    @Before
	public void setUp(){
    	ApplicationContext ac =
		new ClassPathXmlApplicationContext("applicationContext.xml");
    	collectionBean=(CollectionBean)ac.getBean("collectionBean");
    }
    
    @Test
    public void testList(){
    	List<String> citys=collectionBean.getCityList();
    	for (String city : citys) {
    		log.info(city);
    	}
    }
    /**
     *  2012-01-19 20:43:07,625 [test.CollectionBeanTest]-[INFO] 北京
		2012-01-19 20:43:07,625 [test.CollectionBeanTest]-[INFO] 上海
		2012-01-19 20:43:07,625 [test.CollectionBeanTest]-[INFO] 北京
		2012-01-19 20:43:07,625 [test.CollectionBeanTest]-[INFO] 广州
     */
    
    
    @Test
    public void testSet(){
    	Set<String> citys=collectionBean.getCitySet();
    	for (String city : citys) {
			  log.info(city);
    	}
    }
    /**
     *  2012-01-19 20:43:48,921 [test.CollectionBeanTest]-[INFO] 北京
		2012-01-19 20:43:48,921 [test.CollectionBeanTest]-[INFO] 上海
		2012-01-19 20:43:48,921 [test.CollectionBeanTest]-[INFO] 广州
     */
    
    
    @Test
    public void testArray(){
    	String[] ctiys=collectionBean.getCityArray();
    	for (String city : ctiys) {
			log.info(city);
		}
    }
    /**
     *  2012-01-19 20:41:43,406 [test.CollectionBeanTest]-[INFO] 北京
		2012-01-19 20:41:43,406 [test.CollectionBeanTest]-[INFO] 上海
		2012-01-19 20:41:43,406 [test.CollectionBeanTest]-[INFO] 广州
		2012-01-19 20:41:43,406 [test.CollectionBeanTest]-[INFO] 北京
     */
    
    @Test
    public void testMap(){
    	Map<Integer,String> citys = collectionBean.getMms();
    	
    	for (Entry<Integer,String> entry : citys.entrySet()) {
			log.info(entry.getKey()+"  "+entry.getValue());
		}
    }
    /**
     *  2012-01-19 20:44:11,640 [test.CollectionBeanTest]-[INFO] 1  如花
		2012-01-19 20:44:11,640 [test.CollectionBeanTest]-[INFO] 2  如来
		2012-01-19 20:44:11,640 [test.CollectionBeanTest]-[INFO] 3  如草
     */
    
    
    @Test
    public void testProps(){
    	Properties props = collectionBean.getProps();
    	 String driverClassName=(String)props.get("driverClassName");
    	 log.info(driverClassName);
    }
    /**
     * 2012-01-19 20:44:36,437 [test.CollectionBeanTest]-[INFO] com.mysql.jdbc.Driver
     */
}


分享到:
评论

相关推荐

    day38 14-Spring的Bean的属性的注入:集合属性的注入

    NULL 博文链接:https://364232252.iteye.com/blog/2369771

    通过@Autowired注解注入bean的顺序,以及@bean注入.rar

    通过@Bean 和spring的factoryBean注入的bean. 以及对应@service注解注入的类 通过@Autowired 注入对象的时间是现根据类型在根据beanName获取的案例集合

    装配bean—集合类型注入值源码

    装配bean——集合类型注入值: 本文介绍数组、list集合、set集合、map集合、properties的注值 博客原文地址:http://blog.csdn.net/tingzhiyi/article/details/52104203

    详解Java的Spring框架中bean的注入集合

    主要介绍了详解Java的Spring框架中bean的注入集合,Spring是Java的SSH三大web开发框架之一,需要的朋友可以参考下

    spring2.5学习PPT 传智博客

    09_Spring如何装配各种集合类型的属性 10_使用构造器装配属性 11_用@Resource注解完成属性装配 12_编码剖析@Resource注解的实现原理 13.@Autowire注解与自动装配 14.让Spring自动扫描和管理Bean 15.使用JDK中...

    Spring相关测试1

    Spring环境搭建、Spring中Bean的配置(Bean注入的2种方式,注入细节(字面值、引用其他bean、内部bean、集合属性、util scheme 定义集合、p命名空间))

    spring4示例代码

    spring-1 演示了使用setter方式及构造器方式创建bean,util:list标签创建集合,p标签简化配置 和依赖注入, 以及bean的autowire和继承与依赖,以及bean的作用域。 spring-2 演示了外部配置文件的引入(connection)...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    SSHnote_Spring基本配置

    SSH笔记-Spring基本配置方法(注入方式、引用bean、级联属性、集合属性、Properties属性、公用的集合bean、p命名空间)

    Spring面试题含答案.pdf

    30. 在 Spring 中如何注入一个 java 集合? 31. 什么是 bean 装配? 32. 什么是 bean 的自动装配? 33. 解释不同方式的自动装配 34.自动装配有哪些局限性? 35. 你可以在 Spring 中注入一个 null 和一个空字符串吗?...

    Spring in Action(第二版 中文高清版).part2

    2.3 注入Bean属性 2.3.1 注入简单的数值 2.3.2 使用其他的Bean 2.3.3 装配集合 2.3.4 装配空值 2.4 自动装配 2.4.1 四种自动装配类型 2.4.2 混合使用自动和手动装配 2.4.3 何时采用自动装配 2.5 控制Bean...

    Spring in Action(第二版 中文高清版).part1

    2.3 注入Bean属性 2.3.1 注入简单的数值 2.3.2 使用其他的Bean 2.3.3 装配集合 2.3.4 装配空值 2.4 自动装配 2.4.1 四种自动装配类型 2.4.2 混合使用自动和手动装配 2.4.3 何时采用自动装配 2.5 控制Bean...

    spring2.5教程(传智)

    04_Spring的三种实例化Bean的方式 05_配置Spring管理的bean的作用域 06_Spring管理的Bean的生命周期 07_编码剖析Spring依赖注入的原理 08_编码剖析Spring装配基本属性的原理 09_Spring如何装配各种集合类

    spring.doc

    拓展spring为类中的属性赋值: 40 小结: 47 面向接口编程: 47 4 面向切面编程 52 4.1 代理模式 52 代理模式拓展: 52 4.1.1 JDK动态代理 58 JDK动态代理拓展: 59 4.1.2 CGLIB做代理 66 CGLIB动态代理拓展: 68 ...

    spring杂谈 作者zhang KaiTao

    1.12 如何为spring代理类设置属性值 1.13 我对SpringDAO层支持的总结 1.14 我对SpringDAO层支持的总结 1.15 我对SpringDAO层支持的总结 1.16 我对Spring 容器管理事务支持的总结 1.17 我对Spring 容器管理事务支持的...

    Spring in Action(第2版)中文版

    16.4.3在jsf页面中使用springbean 16.4.4在jsf中暴露应用程序环境 16.5spring中带有dwr的支持ajax的应用程序 16.5.1直接web远程控制 16.5.2访问spring管理的beandwr 16.6小结 附录a装配spring a.1下载spring ...

    Spring攻略(第二版 中文高清版).part1

    4.2 将Spring Bean注入脚本中 157 4.2.1 问题 157 4.2.2 解决方案 157 4.2.3 工作原理 157 4.3 从脚本中刷新Bean 160 4.3.1 问题 160 4.3.2 解决方案 160 4.3.3 工作原理 160 4.4 定义内联脚本源码...

    Spring.zip

    Spring注入基本值,Spring注入集合和数组,利用Spring初始化数据库连接池,Spring 表达式读取对象(Bean、数组、Map、Properties)的属性值,bean的注入方式等。

    Spring攻略(第二版 中文高清版).part2

    4.2 将Spring Bean注入脚本中 157 4.2.1 问题 157 4.2.2 解决方案 157 4.2.3 工作原理 157 4.3 从脚本中刷新Bean 160 4.3.1 问题 160 4.3.2 解决方案 160 4.3.3 工作原理 160 4.4 定义内联脚本源码...

Global site tag (gtag.js) - Google Analytics