首页 > 代码库 > web项目中 集合Spring&使用junit4测试Spring

web项目中 集合Spring&使用junit4测试Spring

web项目中 集合Spring

问题:

如果将

   ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

   HelloService helloService = (HelloService) applicationContext.getBean("helloService");

   helloService.sayHello();

直接放入Servlet ,造成每次访问都会加载Spring配置文件,都会创建Spring容器环境 (性能问题)

 

如何确保Spring加载代码 只执行一次??

   ServletContextListener , 启动服务器加载Spring环境,只需要将加载容器 保存ServletContext 范围

 WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
 HelloService helloService = (HelloService) applicationContext.getBean("helloService");

  helloService.sayHello();

1、导入:

?spring-beans-3.2.0.RELEASE.jar
?spring-context-3.2.0.RELEASE.jar
?spring-core-3.2.0.RELEASE.jar
?spring-expression-3.2.0.RELEASE.jar
 
?spring-web-3.2.0.RELEASE.jar
 
?com.springsource.org.apache.commons.logging-1.1.1.jar
?com.springsource.org.apache.log4j-1.2.15.jar
 
2、配置web.xml
 
 public class ContextLoaderListener extends ContextLoader implements ServletContextListener
技术分享
 1   <!-- 注册Spring 监听器 -->
 2    <listener>
 3       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 4   </listener>
 5   
 6   <!-- 加载配置文件默认 WEB-INF/applicationContext.xml -->
 7   <context-param>
 8       <param-name>contextConfigLocation</param-name>
 9       <param-value>classpath:applicationContext.xml</param-value>
10   </context-param>
11   
12    <!--配置Servlet-->
13   <servlet>
14       <servlet-name>HelloServlet</servlet-name>
15       <servlet-class>cn.itcast.web.HelloServlet</servlet-class>
16   </servlet>
17   
18   <servlet-mapping>
19       <servlet-name>HelloServlet</servlet-name>
20       <url-pattern>/hello</url-pattern>
21   </servlet-mapping>

3、编写测试代码

HelloService.java

1 public class HelloService {
2     public void sayHello(){
3         System.out.println("hello spring web!!!");
4     }
5 }

 

技术分享

HelloServlet.java

 1 public class HelloServlet extends HttpServlet {
 2     @Override
 3     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
 4             throws ServletException, IOException {
 5         
 6         //调用HelloService  每次都会加载 applicationContext.xml文件
 7 //        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
 8 //        HelloService helloService = (HelloService) applicationContext.getBean("helloService");
 9 //        helloService.sayHello();
10         
11         
12         // 从ServletContext 范围获得Spring 上下文
13 
14         // 第一种
15         // WebApplicationContext applicationContext = (WebApplicationContext)
16         // getServletContext()
17         // .getAttribute(
18         // WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
19 
20         // 第二种
21         WebApplicationContext applicationContext = WebApplicationContextUtils
22                 .getWebApplicationContext(getServletContext());
23 
24         HelloService helloService = (HelloService) applicationContext
25                 .getBean("helloService");
26         helloService.sayHello();
27         
28     }
29     
30     @Override
31     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
32             throws ServletException, IOException {
33         // TODO Auto-generated method stub
34         super.doPost(req, resp);
35     }
36 }

4、applicationContext.xml

1 <bean id="helloService" class="cn.itcast.service.HelloService"></bean>

5、测试:

http://localhost:8080/spring_web/hello

 
使用junit4测试Spring
在上面jar包基础上加上:spring-test-3.2.0.RELEASE.jar
可以复用上面的,Helloservice.java
测试代码:
 1 // 测试Spring
 2 @RunWith(SpringJUnit4ClassRunner.class)
 3 // 集成插件类
 4 @ContextConfiguration(locations = "classpath:applicationContext.xml")
 5 // 加载配置文件
 6 public class SpringTest {
 7 
 8     @Autowired
 9     private HelloService helloService;
10 
11     @Test
12     public void demo() {
13         helloService.sayHello();
14     }
15 
16     @Test
17     // 没有使用Spring 测试 插件写法
18     public void demo1() {
19         ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
20                 "applicationContext.xml");
21         HelloService helloService = (HelloService) applicationContext
22                 .getBean("helloService");
23         helloService.sayHello();
24     }
25 }

 

 

 

web项目中 集合Spring&使用junit4测试Spring