您的位置:首页 > 编程语言 > Java开发

(3) spring注入Properties,List,Set,Map

2017-07-04 11:00 531 查看
在spring中,除了注入bean对象外,还可以注入Properties,List,Set,Map。

eg:

1. 创建ICollocationService.java接口

package com.lanhuigu.spring.service;

public interface ICollocationService {
String testInjectCollocation();
}
2. 创建CollocationServiceImpl.java实现类
package com.lanhuigu.spring.service.impl;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import com.lanhuigu.spring.dao.CollocationDAO;
import com.lanhuigu.spring.service.ICollocationService;

public class CollocationServiceImpl implements ICollocationService {

/** set注入 */
private CollocationDAO collocationDAO;
/** Properties注入 */
private Properties userEntity;
/** List注入 */
private List userList;
/** Set注入 */
private Set userSet;
/** Map注入 */
private Map userMap;

public void setCollocationDAO(CollocationDAO collocationDAO) {
this.collocationDAO = collocationDAO;
}

public void setUserEntity(Properties userEntity) {
this.userEntity = userEntity;
}

public void setUserList(List userList) {
this.userList = userList;
}

public void setUserSet(Set userSet) {
this.userSet = userSet;
}

public void setUserMap(Map userMap) {
this.userMap = userMap;
}

@Override
public String testInjectCollocation() {
// properties处理
System.out.println("userEntity:"
+ userEntity.getProperty("username") + " " + userEntity.getProperty("password"));
// List处理
System.out.println("userList:" + userList);
for (int i=0;i<userList.size();i++) {
System.out.print(userList.get(i) + " ");
}
System.out.println("");
// Set处理
System.out.println("userSet:" + userSet );
Iterator it = userSet.iterator();
while (it.hasNext()) {
System.out.print(it.next() + " ");
}
System.out.println("");
// Map处理
System.out.println("userMap:" + userMap);
System.out.println("username:" + userMap.get("username")
+ " "
+ "password:" + userMap.get("password") );

return collocationDAO.queryCollocationTest();
}
}

3. 模拟DAO,创建CollocationDAO.java类

package com.lanhuigu.spring.dao;

public class CollocationDAO {
public String queryCollocationTest() {

return "Spring集合注入测试!";
}
}

4. spring  applicationContext.xml配置(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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- ****************************properties,list,set,map注入******************************** -->
<bean id="collocationService" class="com.lanhuigu.spring.service.impl.CollocationServiceImpl">
<!-- Properties注入: results in a setUserEntity(java.util.Properties) call -->
<property name="userEntity">
<props>
<prop key="username">one</prop>
<prop key="password">123456</prop>
</props>
</property>
<!-- List注入: results in a setUserList(java.util.List) call-->
<property name="userList">
<list>
<value>one</value>
<value>two</value>
<value>three</value>
</list>
</property>
<!-- Set注入: results in a setUserSet(java.util.Set) call -->
<property name="userSet">
<set>
<value>one</value>
<value>two</value>
<value>three</value>
</set>
</property>
<!-- Map注入: results in a setUserMap(java.util.Map) call -->
<property name="userMap">
<map>
<entry key="username" value="one" />
<entry key="password" value="123456"/>
</map>
</property>
<!-- set注入 -->
<property name="collocationDAO" ref="collocationDAO" />
</bean>

<bean id="collocationDAO" class="com.lanhuigu.spring.dao.CollocationDAO"/>

</beans>
5. java测试

package com.lanhuigu.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.lanhuigu.spring.service.ICollocationService;

public class SpringColInjectTest {

public static void main(String[] args) {
// 读取spring配置文件
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取bean
ICollocationService collocationService = (ICollocationService)context.getBean("collocationService");
// 调用接口方法
System.out.println(collocationService.testInjectCollocation());
}
}

6. 控制台输出结果

userEntity:one 123456

userList:[one, two, three]

one two three

userSet:[one, two, three]

one two three

userMap:{username=one, password=123456}

username:one password:123456

Spring集合注入测试!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: