环境: MacOS + IntelliJ IDEA 2019.3.1 (Ultimate Edition)
基于手动创建Spring项目结构,整合Spring Web。
1、在pom.xml中增加spring-web依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
2、在web.xml中引入spring-context
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
3、实现ApplicationContextAware和DisposableBean接口,获取ApplicationContext中所有的bean。
package com.codeonline.cats.commons.context;
import com.sun.media.jfxmediaimpl.MediaDisposer.Disposable;
import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* @author 码出高薪
* @Desc. 获取bean 实例
* @date 2020/1/13 08:52
*/
public class SpringContext implements ApplicationContextAware, Disposable {
private static final Logger logger = LoggerFactory.getLogger(SpringContext.class);
private static ApplicationContext applicationContext;
/**
* 根据beanId 获取实例
* @param beanId
* @param <T>
* @return
*/
public static <T> T getBean(String beanId){
assertContextInjected();
return (T) applicationContext.getBean(beanId);
}
/**
* 根据clazz 获取实例
* @param clazz
* @param <T>
* @return
*/
public static <T> T getBean(Class<T> clazz){
assertContextInjected();
return applicationContext.getBean(clazz);
}
/**
* 清空ApplicationContext.
*/
public void dispose() {
logger.debug("清空ApplicationContext");
applicationContext = null;
}
/**
* 实现ApplicationContextAware接口, 注入Context到静态变量中.
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContext.applicationContext = applicationContext;
}
/**
* 检查ApplicationContext不为空.
*/
private static void assertContextInjected(){
Validate.validState(applicationContext != null, "在spring-context中没有定义SpringContext对象.");
}
}
4、在service 目录下创建MyUserService接口
package com.codeonline.cats.service;
/**
* @author 码出高薪
* @Desc. 创建MyUserService接口
* @date 2020/1/13 11:04
*/
public interface MyUserService {
String sayHello();
}
5、在service 目录中创建impl
选择service->右键
->New
->Package
->impl
6、在impl 目录中创建MyUserService接口实现类MyUserServiceImpl
package com.codeonline.cats.service.impl;
import com.codeonline.cats.service.MyUserService;
/**
* @author 码出高薪
* @Desc. 创建MyUserService接口实现类
* @date 2020/1/13 11:10
*/
public class MyUserServiceImpl implements MyUserService {
public String sayHello() {
return "Hello, Spring Web ";
}
}
7、在spring-context 增加bean,myUserService 和springContext
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Service -->
<bean id="myUserService" class="com.codeonline.cats.service.impl.MyUserServiceImpl"/>
<!-- Spring Context -->
<bean id="springContext" class="com.codeonline.cats.commons.context.SpringContext"/>
</beans>
8、在Controller 创建MyUserServiceController
package com.codeonline.cats.web.controller;
import com.codeonline.cats.commons.context.SpringContext;
import com.codeonline.cats.service.MyUserService;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* @author 码出高薪
* @Desc. 调用MyUserService中的sayHello()
* @date 2020/1/13 11:18
*/
public class MyUserServiceController extends HttpServlet {
/**
* 获取Spring容器初始化bean, MyUserService
*/
private MyUserService myUserService = SpringContext.getBean("myUserService");
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String hello = myUserService.sayHello();
req.setAttribute("hello",hello);
req.getRequestDispatcher("index.jsp").forward(req,resp);
}
}
10、 在webapp目录下创建index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>整合Spring web</title>
</head>
<body>
${hello}
</body>
</html>
11、在web.xml中增加servlet
<servlet>
<servlet-name>MyUserServiceController</servlet-name>
<servlet-class>com.codeonline.cats.web.controller.MyUserServiceController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyUserServiceController</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
12、运行项目,在浏览器中输入http://localhost:8080/hello
至此,完成Spring web 整合