Spring集成web环境
1.首先创建一个最简单的maven项目,不要用原型
2.在项目结构中,对模块添加web选项,选择好web-inf和xml部署的位置
https://blog.csdn.net/qq_37112085/article/details/108303613?utm_term=maven%E9%A1%B9%E7%9B%AE%E5%A6%82%E4%BD%95%E6%B7%BB%E5%8A%A0webapp&utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~sobaiduweb~default-1-108303613&spm=3001.4430
3.webapp中添加index.jsp文件
4.pom.xml中添加依赖
pom.xml中添加依赖
<!--添加web层组件-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
5.编辑配置好tomat
避免多次加载配置文件(读取上下文对象ApplicationContext)
在Web项目中,可以使用ServletContextListener
监听Web应用的启动
在Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicationContext
,在将其存储到最大的域servletContext
域中
这样就可以在任意位置从域中获得应用上下文ApplicationContext
对象了。
自定义ContextLoaderListener获取应用上下文
web.xml
<!--配置监听器-->
<listener>
<listener-class>com.itheima.listener.ContextLoaderListener</listener-class>
</listener>
<!--全局初始化参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>applicationContext.xml</param-value>
</context-param>
<!--当使用其他名称配置文件,只要修改web.xml内容即可-->
UserServlet
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
ServletContext servletContext = this.getServletContext();
//ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
ApplicationContext app = WebApplicationContextUtil.getWebApplicationContext(servletContext);
UserService userService = app.getBean(UserService.class);
userService.save();
}
}
WebApplicationContextUtil
/*
解除ContextLoaderListener中
servletContext.setAttribute("app",app)
和UserServlet
ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
耦合问题
*/
/*
将其中servletContext传过来
单独创建工具类解决,修改UserServlet代码
*/
public class WebApplicationContextUtil {
public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
return (ApplicationContext) servletContext.getAttribute("app");
}
}
ContextLoaderListener
public class ContextLoaderListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
//读取web.xml中的全局参数
String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
ApplicationContext app=new ClassPathXmlApplicationContext(contextConfigLocation);
//将Spring的应用上下文对象存储到ServletContext域中
servletContext.setAttribute("app",app);
System.out.println("spring容器创建完毕");
}
public void contextDestroyed(ServletContextEvent servletContextEvent) {}
}
Spring提供获取应用上下文工具
Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装
使用步骤
- 在web.xml中配置
ContextLoaderListener
监听器(导入spring-web坐标)
导入坐标过程中网络中段导致一直无法正常添加依赖解决方法
http://www.bubuko.com/infodetail-3145893.html
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
- 使用
WebApplicationContextUtils
获取应用上下文对象ApplicationContext
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>
UserServlet中直接使用
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext=this.getServletContext();
WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
UserService userService = app.getBean(UserService.class);
userService.save();
}
}