spring整合servlet
新建maven项目或者模块,如果是模块,parent必须是None
在main文件夹下创建这些文件夹和文件
在pom.xml导入相关依赖,有不需要的自己移除
<!--指定jdk版本等-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.2.5.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--spring集成第三方插件的模块,定时函数,邮件服务,读取properties配置文件等-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--spring整合jdbc的模块-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--spring的测试模块-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--spring整合web的模块-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
<!--数据库连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
<!--oracle驱动-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>
<!--aop框架-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
<!--junit测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<!--配置日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<!--servlet依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!--JSTL依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--tomcat7插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
在resources文件夹创建bean.xml(名字随便取)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--扫描com包下的注解-->
<context:component-scan base-package="com.*"/>
</beans>
创建测试的类
在service包下创建测试用的接口
public interface StudentService {
void test();
}
在impl里面实现接口
@Service
public class StudentServiceImpl implements StudentService {
@Override
public void test() {
System.out.println("测试方法");
}
}
在web.xml中配置一下
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
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"
version="4.0">
<!--默认请求Test这个路径-->
<welcome-file-list>
<welcome-file>Test</welcome-file>
</welcome-file-list>
<!--监听服务器的启动与销毁-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--从这个路径获取-->
<context-param>
<param-name>contextConfigLocation</param-name>
<!--在这里写你的配置文件的路径-->
<param-value>classpath:bean.xml</param-value>
</context-param>
</web-app>
再在controler包下创建测试的servlet
@WebServlet("/Test")
public class TestServlet extends BaseServlet {
@Autowired
StudentService studentService;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("接收请求");
studentService.test();
resp.sendRedirect("index.jsp");
}
}
这里是间接继承了HttpServlet,因为要使用注解配置要重写init方法,为了减少代码量,写了一个工具servlet用于重写init方法,我们其他的servlet可以继承这个工具servlet
util包下的BaseServlet
public class BaseServlet extends HttpServlet {
@Override
public void init() throws ServletException {
WebApplicationContext wc = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
//studentService = wc.getBean(StudentService.class);这是不使用注解时的写法,从wc中获取bean直接赋值,但通过工具类不能这么写
AutowireCapableBeanFactory autowireCapableBeanFactory = wc.getAutowireCapableBeanFactory();
autowireCapableBeanFactory.autowireBean(this);
}
}
随便在index.jsp下写一些测试数据
最后就是启动了
在运行按钮旁有个下拉箭头,选择Edit Configurations..
点这个加号
然后选择maven
给命令起个名字,
然后旁边选择我们的刚刚创建的项目,
在Command line:栏输入tomcat7:run
最后点下面的ok,
启动的时候按启动按钮就好啦