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

本文展示了如何使用Spring框架进行依赖注入的两种方式:Setter注入和构造器注入,并提供了具体的测试用例实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编写 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>
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值