當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring集成web环境(使用封装好的工具)
生活随笔
收集整理的這篇文章主要介紹了
Spring集成web环境(使用封装好的工具)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
接上文spring集成web環境(手動實現)
##########代碼接上文#############
spring提供了一個監聽器ContextLoaderListener對上述功能的封裝,該監聽器內部加載spring配置文件,創建應用上下文對象,并存儲到ServletContext域中,提供了一個客戶端工具WebApplicationContextUtils供使用者獲取上下文對象
1.導入spring-web坐標
<dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.2.9.RELEASE</version></dependency>2.在web.xml中配置ContextLoaderListener監聽器
<!-- 配置監聽器,spring-web提供的--><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>3.使用WebApplicationContextUtils獲取上下文對象ApplicationContext
public class UserServlet extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) {ServletContext servletContext = this.getServletContext();ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);UserService userService = app.getBean(UserService.class);userService.save1();}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doPost(request,response);} }4.啟動服務器,訪問userServlet
結果:sava running . . .
總結
以上是生活随笔為你收集整理的Spring集成web环境(使用封装好的工具)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring集成web环境(手动实现)
- 下一篇: SpringMVC-快速入门