spring source of learning the basic configuration file and use the test unit

1. ApplicationContext.xml spring global configuration file is used to control the spring characteristics

  dispatcher-servlet.xml is spring mvc inside the controller, intercept uri forward view

  When using applicationContext.xml file is necessary to add the listener in web.xml

2. idea how to use the test when writing unit tests spring:

The easiest way, without (UnitTestBean)

The first, applicationContext.xml must be placed under 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());
    }
}

The second, applicationContext.xml placed in the WEB-INF (idea default configuration)

ApplicationContext context = new ClassPathXmlApplicationContext("WEB-INF/applicationContext.xml");

Attach the most concise content instance 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>

Plus a web.xml configuration attach content

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

 

Guess you like

Origin www.cnblogs.com/natavidad/p/11305847.html