您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
04-Spring集成web环境-自定义ContextLoaderListener2(从web.xml中获取Spring Config配置文件的文件名)
发布时间:2024-12-25 23:06:35编辑:雪饮阅读()
-
在上篇中有两处还需要优化,一处是监听上下文初始化的实现这里
public void contextInitialized(ServletContextEvent servletContextEvent) {
ClassPathXmlApplicationContext app=new ClassPathXmlApplicationContext("JDBCApplicationContext.xml");
servletContextEvent.getServletContext().setAttribute("app",app);
}
这里很显然固定写死了字符串JDBCApplicationContext.xml,这个其实也可以是作为配置项目的,而不用写到代码里面来。
这里使用的思路是从web.xml里面进行读取。
web.xml里面新增配置如
<context-param>
<param-name>applicationConfig</param-name>
<param-value>JDBCApplicationContext.xml</param-value>
</context-param>
那么此处监听上下文初始化的地方就可以修改实现为
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext servletContext= servletContextEvent.getServletContext();
String applicationConfig=servletContext.getInitParameter("applicationConfig");
ClassPathXmlApplicationContext app=new ClassPathXmlApplicationContext(applicationConfig);
servletContext.setAttribute("app",app);
}
还有第二处需要优化的就是servlet作为是相当于客户端,但上篇中我们写的是这样
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response){
ClassPathXmlApplicationContext app=(ClassPathXmlApplicationContext)request.getServletContext().getAttribute("app");
ComboPooledDataSource bean=app.getBean(ComboPooledDataSource.class);
try{
Connection connection = bean.getConnection();
System.out.println("连接信息:"+connection);
connection.close();
}
catch (Exception e){
e.printStackTrace();
}
}
这里获取上下文的时候也是写死老大app(getAttribute("app")),这样也不友好,对于客户端是没有必要知道你后面的细节的,可以封装一个静态方法来获取,如:
package com.listener;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.ServletContext;
public class WebApplicationUtils {
public static ClassPathXmlApplicationContext getContext(ServletContext servletContext){
return (ClassPathXmlApplicationContext)servletContext.getAttribute("app");
}
}
那么接下来我们的servlet应该实现如:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response){
ClassPathXmlApplicationContext app= WebApplicationUtils.getContext(request.getServletContext());
ComboPooledDataSource bean=app.getBean(ComboPooledDataSource.class);
UserService userService=app.getBean(UserService.class);
userService.save();
try{
Connection connection = bean.getConnection();
System.out.println("连接信息:"+connection);
connection.close();
}
catch (Exception e){
e.printStackTrace();
}
}
关键字词:web,xml,web.xml,spring,ContextLoaderListener,config,文件,文件名
上一篇:03-Spring集成web环境-自定义ContextLoaderListener1(解决IntelliJ IDEA项目不同步到tomcat的webapps目录)
下一篇:05-Spring集成web环境-Spring集成web环境代码实现(从web.xml中获取Spring Config配置文件的文件名的spring官方正统实现方式)
相关文章
- 03-Spring集成web环境-自定义ContextLoaderListener1(
- 01-Spring集成web环境-基本三层架构环境搭建(浏览器访
- 16-Spring集成Junit-代码实现(优化测试需要获取Spring
- 14-Spring注解开发-新注解详解(全注解类代替Spring Co
- 10-Spring注解开发-原始注解入门操作(SpringConfig配
- 09-Spring注解开发-完善测试环境
- 07-Spring配置数据源-Spring(配置文件中)加载properti
- 06-Spring配置数据源-Spring产生数据源对象(SpringCon
- 05-Spring配置数据源-抽取jdbc.properties文件
- 04-Spring配置数据源-手动创建Druid数据源