spring源碼學(xué)習(xí)之基礎(chǔ)配置文件及測試單元使用
1. ApplicationContext.xml 是spring 全局配置文件,用來控制spring 特性的
dispatcher-servlet.xml 是spring mvc里面的,控制器、攔截uri轉(zhuǎn)發(fā)view
使用applicationContext.xml文件時是需要在web.xml中添加listener的
2. idea在寫spring時如何使用test測試單元:
最簡便方法,不加(UnitTestBean)
第一種,applicationContext.xml必須放在src下
@RunWith(BlockJUnit4ClassRunner.class) public class testdemo { @Test public void testdemolador(){ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); testbean a = (testbean)context.getBean("testbean"); System.out.println(a.testdemolodar()); } }
第二種,applicationContext.xml放在WEB-INF下(idea默認配置)
ApplicationContext context = new ClassPathXmlApplicationContext("WEB-INF/applicationContext.xml");
附上applicationContext.xml最簡潔內(nèi)容實例
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" 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 = "testbean" class="bean.testbean" /> </beans>
加一附上web.xml配置內(nèi)容
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
浙公網(wǎng)安備 33010602011771號