Spring_Spring_教程8_注入_复杂类型的注入

本文介绍如何使用Spring框架的XML配置方式来注入不同类型的依赖,包括简单类型、List、Set、Map及Properties等,并通过示例代码展示配置方法。


整体的项目代码下载地址 http://pan.baidu.com/s/1dFFfedN


  简单的来说:

  List是一种按照序号标识的集合,

  Set与List相似但是元素不允许重复,

  Map则是一种自定的键值对,键值都可以是任意的类型。

       Properties常用来编写简单的配置文件  如 db.password=123456


测试项目的工程结构



注意:由于通过property 都是通过 getter,setter 方法 调用反射实现注入,因此需要生成属性的getter,setter方法,

且property中的name属性需要与要注入的类属性名字一致(具体查看最下面的整体Java代码)


下面演示Spring 的 xml项目配置

简单类型的注入

<property name="beanId" value="220"/>



List的注入

<property name="stringList">
			<list>
				<value>1</value>
				<value>2</value>
			</list>	
		</property>


Set类型的注入

<property name="stringSet">
			<set>
				<value>1</value>
				<value>2</value>
			</set>
		</property>


Map类型的注入

<property name="stringMap">
			<map>
				<entry>
					<key>
						<value>sss</value>
					</key>
					<value>Hello world</value>
				</entry>
				<entry>
					<key>
						<value>gkm</value>
					</key>
					<value>Hello world</value>
				</entry>
			</map>
		</property>


Properties类型的注入

<property name="stringProperties">
			<props>
				<prop key="szh">Hello gkm</prop>
				<prop key="gkm">Hello gkm</prop>
			</props>
		</property>


利用JUnit进行测试

UserDao.java

package com.szh.dao;

import com.szh.bean.User;

public interface UserDao {

	void addUser(User u);
	
}


UserDaoImpl.java

package com.szh.dao.impl;

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

import com.szh.bean.User;
import com.szh.dao.UserDao;

public class UserDaoImpl implements UserDao{

	private Integer beanId;
	
	private Set<String> stringSet;
	
	private List<String> stringList;
	
	private Map<String,String> stringMap;
	
	private Properties stringProperties;
	
	public Integer getBeanId() {
		return beanId;
	}

	public void setBeanId(Integer beanId) {
		this.beanId = beanId;
	}

	public Set<String> getStringSet() {
		return stringSet;
	}

	public void setStringSet(Set<String> stringSet) {
		this.stringSet = stringSet;
	}

	public List<String> getStringList() {
		return stringList;
	}

	public void setStringList(List<String> stringList) {
		this.stringList = stringList;
	}

	public Map<String, String> getStringMap() {
		return stringMap;
	}
	
	public void setStringMap(Map<String, String> stringMap) {
		this.stringMap = stringMap;
	}

	
	public Properties getStringProperties() {
		return stringProperties;
	}

	public void setStringProperties(Properties stringProperties) {
		this.stringProperties = stringProperties;
	}

	@Override
	public void addUser(User u) {
		System.out.println("add a user!");
		
	}

	public static void main(String[] args) {
		UserDao test = new UserDaoImpl();
		User u = new User();
		test.addUser(u);
	}
}


UserDaoTest.java(Junit测试)

package test.szh.dao.impl;

import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Properties;

import org.junit.Before;
import org.junit.Test;


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

import com.szh.bean.User;
import com.szh.dao.UserDao;
import com.szh.dao.impl.UserDaoImpl;

public class UserDaoImplTest {

	private ApplicationContext applicationContext;
	
	@Before
	public void setUp() throws Exception {
		this.applicationContext = new ClassPathXmlApplicationContext("beans.xml");
	}

	
	@Test
	public void testAddUser() {		
		UserDao test = (UserDao)this.applicationContext.getBean("u");
		
		System.out.println("UserDaoImpl中的beanId为:"+((UserDaoImpl) test).getBeanId());
//		User u = new User();
//		test.addUser(u);
		
		for(String x: ((UserDaoImpl)test).getStringSet()){
			System.out.println("Set中的数据有:"+x);
		}
		
		for(String x: ((UserDaoImpl)test).getStringList()){
			System.out.println("List中的数据有:"+x);
		}
		
		for(String x: ((UserDaoImpl)test).getStringMap().keySet()){
			System.out.println("Map中的数据有: key:"+x + " value:"+((UserDaoImpl)test).getStringMap().get(x));
		}
		
		Properties properties = ((UserDaoImpl)test).getStringProperties();
		Iterator<?> it = properties.entrySet().iterator();
		while(it.hasNext()){
			 Entry<?, ?> e = (Entry<?, ?>)it.next();
	         System.out.println(e.getKey() + ": " + e.getValue());
		}
	}

}


整体的配置文件

<?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-2.5.xsd">
	<bean name="u" class="com.szh.dao.impl.UserDaoImpl">
		<property name="beanId" value="220"/>
		<property name="stringSet">
			<set>
				<value>1</value>
				<value>2</value>
			</set>
		</property>
		<property name="stringList">
			<list>
				<value>1</value>
				<value>2</value>
			</list>	
		</property>
		<property name="stringMap">
			<map>
				<entry>
					<key>
						<value>sss</value>
					</key>
					<value>Hello world</value>
				</entry>
				<entry>
					<key>
						<value>gkm</value>
					</key>
					<value>Hello world</value>
				</entry>
			</map>
		</property>
		<property name="stringProperties">
			<props>
				<prop key="szh">Hello gkm</prop>
				<prop key="gkm">Hello gkm</prop>
			</props>
		</property>
		
	</bean>
	
	<bean id="userService" class="com.szh.service.UserService">
		<constructor-arg>
			<ref bean="u"/>
		</constructor-arg>
		<!-- <property name="userDao" ref="u"/> -->
	</bean>
	
	
</beans>


测试输出





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值