1、整合Junit
- 导入jar包:
基本:4+1
测试:spring-test....jar
- 让Junit去通知spring加载配置文件
- 让spring容器自动进行注入
- 添加三个注解:@RunWith、@ContextConfiguration、@Autowired
package junit_test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; 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 service.AccountService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class TestApp { @Autowired private AccountService accountService;//与Junit整合,不需要在spring xml中配置扫描 @Test public void demo(){ //ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); //AccountService accountService = applicationContext.getBean("accountService",AccountService.class); accountService.transfer("jack","rose",30); } }
2、整合web
1、tomcat启动加载配置文件