1.导包
导入spring-test包
2.在Person中添加注解
import com.Spring.pojo.Person; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml")
3.创建测试类
package com.spring.pojo; import javax.annotation.Resource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.Spring.pojo.Person; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class RunWithTest { @Resource(name="person") private Person p; @Test @SuppressWarnings("resource") public void testProperty() { AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-injection.xml"); Person person1=(Person) context.getBean("person1"); System.out.println(person1); } }