Spring笔记(4)
集成Web环境
1.步骤
-
导入Spring-web坐标
<!-- spring-web--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.0.5.RELEASE</version> </dependency>
-
将ApplicationContext.xml的文件名称存入web.xml的全局参数中
<!-- 全局初始化参数--> <!--applicationContext文件名--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param>
-
配置监听器
<!-- 配置监听器--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
-
编写Servlet
-
使用WebApplicationContextUtils获取ApplicationContext(获取的为子类WebApp..)
//获取app对象(使用工具包中的方法) ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
2.pom.xml
<!-- servlet-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- servlet-jsp-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<!-- spring-context-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<!-- jdbc-druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!-- jdbc-c3p0-->
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- spring-web-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</plugin>
3.servlet具体实现
3.1工具包实现:(封装好的)
- servlet中的app通过工具包中的方法获得()
- 在servletContext域中获得app对象
- 域中app对象在监听器模块内部实现存储
public class ApplicationContextLoaderUtil {
public static ApplicationContext getApplicationContext(ServletContext servletContext){
//在域中获取applicationContext对象
ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
//测试
System.out.println("通过工具类获取app");
return app;
}
}
3.2监听器实现(封装好的)
手动配置(web.xml中)
<!-- 全局初始化参数-->
<!--applicationContext文件名-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 配置监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
内部封装好
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//在servletContext中获取ApplicationContext对象
//获取servletContext
ServletContext servletContext = request.getServletContext();
//获取app对象(使用工具包中的方法)
ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
//获取userService
UserService userService = app.getBean(UserService.class);
userService.save();
4.注意
配置pom.xml时:
servlet-api和servlet-jsp-api时应该加入范围
<scope>provided</scope>
applicationContext.xml中:
添加扫描注解
<context:component-scan base-package="dao"/>
<context:component-scan base-package="service"/>