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=土豪金]
销毁对象