日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

spring整合web

發布時間:2025/3/15 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring整合web 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

既然說到web自然就有servlet:

public class UserServlet extends HttpServlet { @SuppressWarnings("resource") public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { /** * 這種整合沒有問題也可以。但是有一個問題每一次請求都加載spring壞境。 * 每一次請求都加載配置文件這 樣是不好的。性能大大的降低 * 也許有人會這樣解決這個問題servlet不是有個init方法嗎? * 將spring運行壞境的加載在init方法中加載這樣 就保證了只加載一次 * 確實是只加載了一次,但是也還存在一個問題,就是這個spring的運行壞境這些配置文件只有這個 * servlet能使用,其他是servlet不能使用。 *? */ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");UserService bean = (UserService) context.getBean("userService");bean.sayHello(); /** * 使用Spring提供的整合web來有兩種方式 * 一般會使用下面的一種方式*將spring的壞境放在ServletContext這樣域中。這個域是全局的。 *每一個加載時隨著tomcat啟動而加載這樣 * 就解決了前面的缺點*但是如何才能將這個spring的壞境放入servletContext域中。 *這時我們需要在web.xml文件中配置一個監聽 器listener, *利用監聽器去監聽這個ServletContext這個域創建和銷毀。*但是還有一個問題.spring壞境默認會到WEB-INF目錄下加載配置文件。 *然而你的配置文件在src下。 如何才能加載這個核心的配置文件這時候就需要配置一個全局的初始化參數告訴spring到src下去加載這個配置文件<!--監聽器配置?--><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>??????? ? ??*//*WebApplicationContext context=WebApplicationContextUtils.getWebApplicationContext(getServletContext());UserService bean = (UserService) context.getBean("userService");bean.sayHello();*/ }



/**
* The doPost method of the servlet. <br>
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}


}

總結

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

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