Spring整合junit测试

Spring整合junit测试
1.导包:
spring-test-4.2.4.RELEASE.jar
2.代码:
2.1测试代码:

package demo;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.itcast.bean.Car;
import cn.itcast.bean.Person;
import junit.framework.JUnit4TestCaseFacade;
//1.创建容器
@RunWith(SpringJUnit4ClassRunner.class)
//2.指定创建容器加载那个文件
@ContextConfiguration("classpath:applicationContext.xml")
public class Demo {
	//3.将名为 person的对象注入到p属性中
	@Resource(name="person")
	private Person p;
	@Resource(name="car")
	private Car c;
	public void fun1() {
		System.out.println(p);
		System.out.println(c);
	}
	
}

2.2applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd " xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<context:component-scan base-package="cn.itcast.bean"></context:component-scan>
</beans>

2.3类的对象代码:

package cn.itcast.bean;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("car")
public class Car {
	@Value("布加迪威龙")
	private String name;
	@Value("土豪金")
	private String color;
	public String getName() {
		return name;
	}
	public String getColor() {
		return color;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setColor(String color) {
		this.color = color;
	}
	@Override
	public String toString() {
		return "Car [name=" + name + ", color=" + color + "]";
	}
	
}

package cn.itcast.bean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Scope;
import org.springframework.core.convert.Property;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

/*@Component("person")*/
//@Service("person")//管理service层对象
/*@Scope(scopeName="prototype")*/
@Controller("person")
public class Person {
	private String name;
	private int age;
	/*@Autowired
	@Qualifier("car2")*/
	@Resource(name="car")//手动注解
	private Car car;
	public String getName() {
		return name;
	}
	public int getAge() {
		return age;
	}
	public Car getCar() {
		return car;
	}
	@Value("张三")
	public void setName(String name) {
		this.name = name;
	}
	@Value("18")
	public void setAge(int age) {
		this.age = age;
	}
	public void setCar(Car car) {
		this.car = car;
	}
	@PostConstruct
	public void init() {
		System.out.println("开始创建");
	}
	@PreDestroy
	public void destry() {
		System.out.println("销毁对象");
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", car=" + car + "]";
	}
	
}

输出结果为:

开始创建
Person [name=张三, age=18, car=Car [name=布加迪威龙, color=土豪金]]
Car [name=布加迪威龙, color=土豪金]
销毁对象
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值