spring在web中的使用
在web项目中使用spring
在之前的示例中,我们都使用的是非web进行测试的,即没有部署到tomcat容器里面,在实际开发中,需要创建web项目,此时只需要将spring容器放到ServletContext域中即可。
添加依赖
导入相关spring-web的jar包,这里只需要在项目中添加下面依赖即可:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
注册监听器 ContextLoaderListener
这里将spring容器放到ServletContext域中,需要在ServletContext初始化的时候添加进去,此时就需要使用监听器接口ServletContextListener 对 ServletContext 进行监听。在 web.xml 中注册该监听器。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
通过源码可以看到spring在ContextLoaderListener的initWebApplicationContext方法中创建了容器对象。
指定spring配置文件的位置
需要在web.xml文件中指定spring配置文件所在的位置
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
这里使用注解的方式,所以在配置文件中添加:
<context:component-scan base-package="com.monkey1024"/>
创建servlet
不要忘了添加下面依赖:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
创建一个servlet在servlet中注入UserService,这里使用以前创建的UserService和UserDao:
package com.monkey1024.servlet;
import com.monkey1024.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
@WebServlet("/springtest")
public class SprintServlet extends javax.servlet.http.HttpServlet {
@Autowired
private UserService userService;
/**
* @param config
* @throws ServletException
*/
public void init(ServletConfig config) throws ServletException {
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
config.getServletContext());
}
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
doGet(request, response);
}
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
userService.addUser();
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.monkey1024</groupId>
<artifactId>02_spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>02_spring</finalName>
<plugins>
<!-- 编译插件,指定编译用的的jdk版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- jdk的版本号 -->
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!-- 注册监听器ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指定spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLoaction</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.monkey1024"/>
</beans>
SpringServlet.java
package com.monkey1024.servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import com.monkey1024.service.UserService;
@WebServlet("/test ")
public class SpringServlet extends HttpServlet{
@Autowired
private UserService userService;
public void init(ServletConfig config) throws ServletException{
//让servlet支持注入
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this, config.getServletContext());
}
protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException{
userService.addUser();
}
protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException{
doPost(request,response);
}
}
UserService.java
package com.monkey1024.service;
public interface UserService {
void addUser();
}
UserServiceImpl.java
package com.monkey1024.service.impl;
import org.springframework.stereotype.Service;
import com.monkey1024.service.UserService;
@Service("UserService")
public class UserServiceImpl implements UserService{
public void addUser(){
System.out.println("用户添加");
}
}