• 在web项目中集成Spring


    web项目中集成Spring 

    一、使用Servlet进行集成测试

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

    1. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
      HelloService helloService = (HelloService) applicationContext.getBean("helloService");
      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>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    默认读取 WEB-INF/applicationContext.xml 

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

    1. <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext.xml</param-value>
      </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. public class HelloService{
          public void sayHello(){
              System.out.println("hello,Spring web");
          }
      }
    2)注册bean:
    1. <?xml version="1.0" encoding="UTF-8"?>
      <beansxmlns="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">
      <beanid="helloService"class="cn.itcast.service.HelloService"></bean>
      </beans>
    3)配置web.xml文件
    1. <?xml version="1.0" encoding="UTF-8"?>
      <web-appversion="2.5"
      xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <!-- 配置Spring 监听器 -->
      <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <!-- 配置Spring配置文件所在位置 -->
      <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:applicationContext.xml</param-value>
      </context-param>
      <display-name></display-name>
      <servlet>
      <servlet-name>HelloServlet</servlet-name>
      <servlet-class>cn.itcast.servlet.HelloServlet</servlet-class>
      </servlet>
      
      <servlet-mapping>
      <servlet-name>HelloServlet</servlet-name>
      <url-pattern>/hello</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
      </web-app>
    4)编写servlet(测试)
    1. public class HelloServletextendsHttpServlet{
        
      public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{    WebApplicationContext applicationContext =WebApplicationContextUtils.getWebApplicationContext(getServletContext()); HelloService helloService =(HelloService) applicationContext.getBean("helloService"); helloService.sayHello(); }
      publicvoid doPost(HttpServletRequest request,HttpServletResponse response)
      throws ServletException,IOException{ doGet(request, response); } }
     

    二、Spring 整合 junit4 测试 

    1、 导入spring-test.jar  

    2、 编写测试用例 

    1. @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(locations = "classpath:applicationContext.xml") // 指定配置文件位置
      public class HelloServiceTest {
          @Autowired
          private HelloService helloService; // 注入需要测试对象
          @Test
          // 测试
          public void demo2() {
              helloService.sayHello(); // 调用测试方法
          }
      }
    博采众长才能相互印证,故步自封必将粗陋浅薄!
  • 相关阅读:
    【node.js】GET/POST请求、Web 模块
    【node.js】全局变量、常用工具、文件系统
    【node.js】函数、路由
    【node.js】模块系统、函数
    【node.js】Stream(流)
    跨域问题的产生,怎么解决它?
    array.splice()
    数组的方法
    js跨域请求的5中解决方式
    3分钟看懂flex布局
  • 原文地址:https://www.cnblogs.com/tangwan/p/4674976.html
Copyright © 2020-2023  润新知