您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
03-Spring集成web环境-自定义ContextLoaderListener1(解决IntelliJ IDEA项目不同步到tomcat的webapps目录)
发布时间:2024-12-25 21:04:15编辑:雪饮阅读()
-
前番我们说是在每次测试的时候还要通过applicationContext来获取bean这种重复冗余的逻辑,那么当时的解决方案是用专门的用于Spring的单元测试结合新注解进行解决的。
那么对于我们非测试,就是如main方法,或者说我们需要在每个servlet中实现的时候就需要另外考虑了。这里用监听器实现。
首先我们假定一个包路径去实现监听器,这里也是在web.xml中配置,并我们利用该监听器在一个servlet被访问前就实现了applicationContext的获取,然后在servlet中时候就不用获取,直接从attribute中取得即可。所以web.xml中再多配置一个servlet,也是先假定一个呗。
那么这里完整的web.xml如:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="4.0"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd">
<display-name>Archetype Created Web Application</display-name>
<listener>
<listener-class>com.listener.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Servlet01</servlet-name>
<servlet-class>com.web.Servlet01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet01</servlet-name>
<url-pattern>/Servlet01</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Servlet02</servlet-name>
<servlet-class>com.web.Servlet02</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet02</servlet-name>
<url-pattern>/Servlet02</url-pattern>
</servlet-mapping>
</web-app>
然后那我们的监听器实现如:
package com.listener;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
public class ContextLoaderListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent servletContextEvent) {
ClassPathXmlApplicationContext app=new ClassPathXmlApplicationContext("JDBCApplicationContext.xml");
servletContextEvent.getServletContext().setAttribute("app",app);
}
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
然后那我们的新建的servlet实现如:
package com.web;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
public class Servlet02 extends HttpServlet {
@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();
}
}
}
接下来咱们需要解决两个问题,第一个问题就是昨天遗留的,当时是intelliJ IDEA上面配置了tomcat,但是当我们项目运行的时候项目并没有自动运行起来,只是tomcat运行起来了,而当时我是手动复制到本地安装的tomcat中的webapps目录中的。
那其实可以一个更简单的方法,在Tomcat配置的Deployment哪里添加Artifact(假如都统一为war类型),并修改Application context的值和该war包打包出去之后的文件名(不包含后缀),然后在Project Structure里面的Artifacts里面找到你Deployment选中的那个artifact在Output directory直接选中到本地安装的tomcat中的webapps目录中。

第二个问题:
当然这个Artifacts中你选中的Artifact右边区域有关Available Elements里面在Artifacts下面(不是其下,而是相邻)应该能找到你当前project或module的名称,在其上右键Put into Output Root。
这样可以解决当你tomcat启动后在IntelliJ IDEA在Tomcat Localhost Log控制台里面的报错如:
java.lang.NoClassDefFoundError: org/springframework/context/support/ClassPathXmlApplicationContext
的问题。
对了,还有一个问题就是tomcat8.5好像是不支持web.xml的4.0,所以web.xml
要修改为如3.1
<web-app
version="3.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:javaee="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
这里只注意两个地方一个是version="3.1",一个是
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
不然会在tomcat启动后在IntelliJ IDEA在output控制台中有类似警告提示。
那么最后你可以访问如
http://localhost:8080/untitled_war/Servlet02
就能在output中看到连接信息了。
关键字词:Spring,web
上一篇:01-Spring集成web环境-基本三层架构环境搭建(浏览器访问并在IntelliJ IDEA控制台打印jdbc连接(从Spring Config配置xml中取到))
下一篇:04-Spring集成web环境-自定义ContextLoaderListener2(从web.xml中获取Spring Config配置文件的文件名)
相关文章
- 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数据源
- 03-Spring配置数据源-数据源的开发步骤和手动创建C3P0