spring属性依赖注入
2015-09-02 19:57
671 查看
一、构造方法方式注入
1、项目结构如下:
beans
4、新建测试类
1、项目结构如下:
<?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"> <!-- 集合内部,普通值用value,引用值用ref --> <bean id="collectionTestId" class="hjp.spring.attributeinject.collection.CollectionTest"> <!-- list集合 --> <property name="listData"> <list> <value>Jim</value> <value>Tom</value> <value>Jerry</value> </list> </property> <!-- set集合 --> <property name="setData"> <set> <value>张三</value> <value>李四</value> <value>王五</value> </set> </property> <!-- map集合 --> <property name="mapData"> <map> <!-- 第一种写法 --> <entry key="A" value="a"></entry> <entry key="B" value="b"></entry> <entry key="C" value="c"></entry> <!-- 第二种写法 --> <entry> <key> <value>D</value> </key> <value>d</value> </entry> </map> </property> <!-- 数组 --> <property name="arrayData"> <array> <value>a</value> <value>b</value> <value>c</value> </array> </property> <!-- Properties --> <property name="propsData"> <props> <prop key="X">x</prop> <prop key="Y">y</prop> <prop key="Z">z</prop> </props> </property> </bean> </beans>
beans
4、新建测试类
package hjp.spring.attributeinject.collection; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestApp { @Test public void demo1() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "hjp/spring/attributeinject/collection/beans.xml"); CollectionTest collectionTest = applicationContext.getBean("collectionTestId", CollectionTest.class); System.out.println(collectionTest); } }
相关文章推荐
- eclipse MAT - Memory Analyzer Tool 使用进阶
- java获取资源路径问题
- mybatis-springmvc+mybatis实现增删改查
- java中equals与==的区别
- java正则表达式最简单 学习教程
- 程序算法之选择排序和冒泡排序
- MyEclipse下java.lang.OutOfMemoryError: Java heap space的解决!
- java线程间通信
- java并发编程第六章(7)生成并发编程随机数
- java优化占用内存的方法(一)
- java并发编程第六章(6)使用线程安全可遍历映射
- java中final static关键字用法
- mybatis错误-org.mybatis.spring.transaction.SpringManagedTransactionFactory.newTransaction
- java工具类之Graphics
- Java并发编程:volatile关键字解析
- Java实现二叉排序树
- Spring SpEL表达式
- java使用Junit工具进行单元测试
- Eclipse中Axis2发布WebService
- 搭建Spring + SpringMVC + Mybatis框架之一(创建项目)