码迷,mamicode.com
首页 > 编程语言 > 详细

06_在web项目中集成Spring

时间:2015-07-24 23:58:49      阅读:367      评论:0      收藏:0      [点我收藏+]

标签:

web项目中集成Spring 

一、使用Servlet进行集成测试

1.直接在Servlet 加载Spring 配置文件

  1. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  2. HelloService helloService = (HelloService) applicationContext.getBean("helloService");
  3. helloService.sayHello();
 

问题: 每次请求都会加载Spring环境,初始化所有Bean ,性能问题 !!!

解决方案一: 将代码放入Servlet init 中 , 无法保证所有Servlet都能使用 ApplicationContext 

解决方案二: ServletContext,在tomcat启动时, 加载Spring配置文件,获得对象 ,放入ServletContext 

* ServletContextListener 

 

2.导入spring-web.jar

保存 ContextLoaderListener 完成在Servlet初始化阶段,加载Spring配置文件,将工厂对象放入 ServletContext 

 

3.配置web.xml 

  1. <listener>
  2.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  3. </listener>
默认读取 WEB-INF/applicationContext.xml 

配置全局参数 contextConfigLocation 指定 配置文件位置 

  1. <context-param>
  2.     <param-name>contextConfigLocation</param-name>
  3.     <param-value>classpath:applicationContext.xml</param-value>
  4. </context-param>
 

4.修改Servlet代码 

ServletContext中获得 Spring工厂 

第一种:

  1. WebApplicationContext applicationContext = (WebApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
 

第二种:

  1. WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
 
**********************************************************************************完整代码*********************************************************************************
1)编写service(bean):
  1. publicclassHelloService{
  2. publicvoid sayHello(){
  3. System.out.println("hello,Spring web");
  4. }
  5. }
2)注册bean:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beansxmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <beanid="helloService"class="cn.itcast.service.HelloService"></bean>
  7. </beans>
3)配置web.xml文件
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-appversion="2.5"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  7. <!-- 配置Spring 监听器 -->
  8. <listener>
  9. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  10. </listener>
  11. <!-- 配置Spring配置文件所在位置 -->
  12. <context-param>
  13. <param-name>contextConfigLocation</param-name>
  14. <param-value>classpath:applicationContext.xml</param-value>
  15. </context-param>
  16. <display-name></display-name>
  17. <servlet>
  18. <servlet-name>HelloServlet</servlet-name>
  19. <servlet-class>cn.itcast.servlet.HelloServlet</servlet-class>
  20. </servlet>

  21. <servlet-mapping>
  22. <servlet-name>HelloServlet</servlet-name>
  23. <url-pattern>/hello</url-pattern>
  24. </servlet-mapping>
  25. <welcome-file-list>
  26. <welcome-file>index.jsp</welcome-file>
  27. </welcome-file-list>
  28. </web-app>
4)编写servlet(测试)
  1. publicclassHelloServletextendsHttpServlet{
  2. publicvoid doGet(HttpServletRequest request,HttpServletResponse response)
  3. throwsServletException,IOException{
  4. WebApplicationContext applicationContext =WebApplicationContextUtils.getWebApplicationContext(getServletContext());
  5. HelloService helloService =(HelloService) applicationContext.getBean("helloService");
  6. helloService.sayHello();
  7. }
  8. publicvoid doPost(HttpServletRequest request,HttpServletResponse response)
  9. throwsServletException,IOException{
  10. doGet(request, response);
  11. }
  12. }
 

二、Spring 整合 junit4 测试 

1、 导入spring-test.jar  

2、 编写测试用例 

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations = "classpath:applicationContext.xml") // 指定配置文件位置
  3. public class HelloServiceTest {
  4.     @Autowired
  5.     private HelloService helloService; // 注入需要测试对象
  6.     @Test
  7.     // 测试
  8.     public void demo2() {
  9.         helloService.sayHello(); // 调用测试方法
  10.     }
  11. }
 

 





06_在web项目中集成Spring

标签:

原文地址:http://www.cnblogs.com/tangwan/p/4674976.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!