日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring整合web开发

發布時間:2023/12/13 javascript 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring整合web开发 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

正常整合Servlet和Spring沒有問題的

public class UserServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService = (UserService) applicationContext.getBean("userService");userService.sayHello();}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);} }

但是每次執行Servlet的時候都要加載Spring配置,加載Spring環境,極大地降低效率!!!

解決辦法
  1:在Servlet的init方法中加載Spring配置文件?(不好)
    當前這個Servlet可以使用,但是其他的Servlet用不了了!!!如果要使用,必須每個Servlet的init方法中都要加載Spring配置文件,太麻煩(pass)
  2:將加載的信息內容放到ServletContext中(正確)
    ServletContext對象是全局的對象.服務器啟動的時候創建的.在創建ServletContext的時候就加載Spring的環境,ServletContextListener用于監聽ServletContext對象的創建和銷毀
    使用方法
      1:導入Spring web開發jar包:spring-web-3.2.0.RELEASE.jar
      2:將Spring容器初始化,交由web容器負責,配置核心監聽器 ContextLoaderListener,配置全局參數contextConfigLocation(用于指定Spring的框架的配置文件位置)
        在web.xml中配置

<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value> </context-param>

        修改程序的代碼

public class UserServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {/*也可用這種方式獲得applicationContext:WebApplicationContext applicationContext = (WebApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);*/WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(getServletContext());UserService userService = (UserService) applicationContext.getBean("userService");userService.sayHello();}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);} }

轉載于:https://www.cnblogs.com/fengmingyue/p/6202892.html

總結

以上是生活随笔為你收集整理的Spring整合web开发的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。