spring源码学习之基础配置文件及测试单元使用

1.     ApplicationContext.xml  是spring 全局配置文件,用来控制spring 特性的

  dispatcher-servlet.xml 是spring mvc里面的,控制器、拦截uri转发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最简洁内容实例

<?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配置内容

<?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>

 

posted @ 2019-08-06 20:54  Natavidad  阅读(356)  评论(0编辑  收藏  举报