1.程序中有Junit环境
2.导入一个jar包.spring与junit整合jar包
spring-test-3.2.0.RELEASE.jar
3.测试代码
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations="classpath:applicationContext.xml") public class SpringTest { @Autowired private UserService userService; @Test public void demo1(){ userService.sayHello(); } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userService" class="cn.yzu.service.UserService"></bean> </beans>