在说spring集成web环境之前,先说一个小问题,之前的学习中,我们发现每段代码都需要运行这两句话
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) context.getBean("userDao");
代码非常冗余,而且配置文件加载多次,应用环境上下文也加载多次。我们就会想,如何提取呢,在JavaSE阶段,提取一般都采用工具类静态方法进行提取,那这个可以吗?当然可以。不过感觉还是有点冗余,每次使用都需要写工具类调用它,这时呢,我们就可以想到javaweb阶段使用到的监听器来加载它,在tomcat启动时,就使用监听器加载spring容器,可以一定程度上提高性能,而且是解耦的。接下来就简单介绍一下提取这个监听器类并配置它。
@WebListener
public class ContextLoaderListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
// 加载spring配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取servletContext容器
ServletContext servletContext = sce.getServletContext();
// 将容器配置到application域中
servletContext.setAttribute("context",context);
}
public void contextDestroyed(ServletContextEvent sce) {
}
}
@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取servletContext容器
ServletContext servletContext = this.getServletContext();
// 从servletContext容器中获取spring容器
ApplicationContext context = (ApplicationContext)servletContext.getAttribute("context");
UserService service = context.getBean(UserService.class);
System.out.println(service);
service.insert();
}
}
可以发现,上述代码已经实现了一次加载,多次使用的基本要求,但是仍然存在一定的耦合,比如配置文件的名称,比如容器的名称必须得让用户记住等。那我们可以继续抽取,解耦。
最终的代码:
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
@WebListener
public class ContextLoaderListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
// 获取servletContext容器
ServletContext servletContext = sce.getServletContext();
//读取web.xml中的全局参数
String configLocation = servletContext.getInitParameter("contextConfigLocation");
ApplicationContext context = new ClassPathXmlApplicationContext(configLocation);
servletContext.setAttribute("context", context);
System.out.println("spring容器初始化……");
}
public void contextDestroyed(ServletContextEvent sce) {
}
}
public class WebApplicationContextUtils {
public static ApplicationContext getApplicationContext(ServletContext servletContext) {
return (ApplicationContext) servletContext.getAttribute("context");
}
}
@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
ApplicationContext context = WebApplicationContextUtils.getApplicationContext(servletContext);
UserService service = context.getBean(UserService.class);
System.out.println(service);
service.insert();
}
}
咱能想到的spring早想到了,并且提供了一个包叫spring-web,它用于spring集成web环境,其中一项功能就是定义了自己的监听器,并实现了上述功能,人家的肯定更为完善好用,接下来简单介绍一下该监听器的使用。
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>5.3.9version>
dependency>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
以上就是spring集成web环境的步骤,希望能对大家有所帮助