您当前的位置: 首页 > 慢生活 > 程序人生 网站首页程序人生
01-Spring集成web环境-基本三层架构环境搭建(浏览器访问并在IntelliJ IDEA控制台打印jdbc连接(从Spring Config配置xml中取到))
发布时间:2024-12-25 02:30:12编辑:雪饮阅读()
-
前番我们用Spring Config无论是配置文件还是配置类都是在模拟的web中在控制台获取bean的去测试的,这次也结合到真实的web环境中。
那么pom依赖也是比上篇中多加一个servlet的依赖咯
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
然后建立servlet
package com.web;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import javax.servlet.http.*;
import java.sql.Connection;
public class Servlet01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response){
ClassPathXmlApplicationContext app=new ClassPathXmlApplicationContext("JDBCApplicationContext.xml");
ComboPooledDataSource bean=app.getBean(ComboPooledDataSource.class);
try{
Connection connection = bean.getConnection();
System.out.println("连接信息:"+connection);
connection.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}
然后这里的web.xml中添加servlet映射
<?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>
<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>
</web-app>
最后将打包后的war文件放到tomcat的webapps目录下启动tomcat服务器
那我这里遇到的几个问题就是无法自动生成war包,就算能自动生成war包,也不会自动放入tomcat的webapps目录里面了,即便是tomcat副本环境例如
C:\Users\1\AppData\Local\JetBrains\IntelliJIdea2020.3\tomcat\edf520bb-f4f1-4655-9a48-b5adffdc29c3\work\Catalina\localhost
这里也不会有了。
那么我是在File=》Project Structure=>Artifacts=>随便那个的Ouput directory的目录里面找到Build之后的文件夹拷贝到我的IntelliJ IDEA关联的我本地的tomcat安装目录里面的webapps目录里面的,那么虽然IntelliJ IDEA不部署,但至少能启动该tomcat即可。
千万别放错位置了,不是IntelliJ IDEA的那个副本的目录
C:\Users\1\AppData\Local\JetBrains\IntelliJIdea2020.3\tomcat\edf520bb-f4f1-4655-9a48-b5adffdc29c3\work\Catalina\localhost
不是这个,而是你本来安装的那个主tomcat目录。如
D:\software\apache-tomcat-8.5.68-windows-x64\apache-tomcat-8.5.68\webapps
Artifacts:制品
Facets: (事物的)方面特征
Structure:构造
关键字词:Spring,web
上一篇:16-Spring集成Junit-代码实现(优化测试需要获取SpringConfig的bean的强依赖单元测试)
下一篇:03-Spring集成web环境-自定义ContextLoaderListener1(解决IntelliJ IDEA项目不同步到tomcat的webapps目录)
相关文章
- 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
- 20-Spring相应API2(getBean通过接口类来获取)