spring怎么和web项目集成
在maven项目里面导入依赖核心包在pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
- web项目如何读取spring上下文,创建SpringWebUtil类(为了将spring上下文整合进tomcat容器)
public class SpringWebUtil {
public static final String SPRING_WEB_KEY = "spring_web_key";
/**
*
* @param applicationContext spring上下文
* @param servletContext tomcat容器
*/
public static void setApplicationContext(ApplicationContext applicationContext, ServletContext servletContext) {
servletContext.setAttribute(SPRING_WEB_KEY, applicationContext);
}
public static ApplicationContext getApplicationContext(ServletContext servletContext) {
return (ApplicationContext) servletContext.getAttribute(SPRING_WEB_KEY);
}
}
- 通过监听器