`
wh007_cx
  • 浏览: 28489 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

【原创】基于spring 3.1.0 两种依赖注入的测试代码

阅读更多

编写 SetterInjection 的测试用例

 

TestSetterInjection 直接继承于 Spring 所提供的 AbstractJUnit4SpringContextTests 的抽象测试类。

 

TestSetterInjection.java代码:

 

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.wh.tech.spring.di.SetterInjection;

/**
 * @Author:WangHuan
 * 
 * @Date:2011-7-1
 * 
 * @TODO:测试Setter方式注入
 */

@ContextConfiguration(locations={"classpath:applicationContext-di.xml"})
public class TestSetterInjection extends AbstractJUnit4SpringContextTests {
	
	@Autowired  //
	private SetterInjection setterInjection;
	
	@Test
	public void getObject(){
		setterInjection.getObject();
	}

}

 

 

 

SetterInjection.java代码:

 

package org.wh.tech.spring.di;


/**
 * @Author:WangHuan
 * 
 * @Date:2011-7-1
 *
 * @TODO:用setter方式注入
 */

public class SetterInjection implements DiService {

	private String arg0;
	private String arg1;
	
	
	public void setArg0(String arg0) {
		this.arg0 = arg0;
	}


	public void setArg1(String arg1) {
		this.arg1 = arg1;
	}


	public String getArg0() {
		return arg0;
	}


	public String getArg1() {
		return arg1;
	}


	/* (non-Javadoc)
	 * @see org.wh.tech.spring.di.DiService#getObject()
	 */
	@Override
	public Class<?> getObject() {
		// TODO Auto-generated method stub
		System.out.println(this.getArg0() + " ; " + this.getArg1());
		return this.getClass();
	}

}
 

applicationContext-di.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" >
	
	<bean id="setterInjection" class="org.wh.tech.spring.di.SetterInjection" scope="prototype">
		<property name="arg0" value="0" />
		<property name="arg1" value="1" />
	</bean>
</beans>
 

 

编写 ConstructorInjection 的测试用例

 

主要差别的配置如下


	<bean id="constructorInjection" class="org.wh.tech.spring.di.ConstructorInjection" scope="prototype">
		<constructor-arg type="java.lang.String">
			<value>test</value>
		</constructor-arg>
		<constructor-arg type="int">
			<value>70</value>
		</constructor-arg>		
	</bean>
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics