这篇内容我们给大家介绍一下Spring框架如何整合Servlet。光看表面现象这个问题感觉没有什么太大难度,但是实际在整合过程中不是那么轻松
既然是以补充的方式来介绍,那么我们就直接上一个案例来说明整合实现的步骤
1. 案例要求
- 通过Spring框架注解方式来打通控制层,业务逻辑层,数据访问层之间的依赖关系
- 控制层采用Servlet来完成对用户请求与相应的处理
- 然后在Servlet中通过@Autowired方式来依赖注入业务逻辑层
- 业务逻辑层也是通过@Autowired方式来依赖注入数据访问层
- 数据访问层采用Spring对JDBC支持方式来获取数据,编辑数据等操作
2. 技术难点
- 如何让Servlet被Spring框架管理
- 如何让Tomcat服务器加载Spring框架(即:如何加载Spring的配置文件)
- 一旦Servlet被Spring框架管理,但是@Autowired注解是不能自动依赖注入业务逻辑层中的对象,这个该如何解决
3. Tomcat服务器加载Spring配置文件的实现步骤
- 导入spring-web.jar包
- 在web.xml文件中添加<listener.../>监听标签
- 利用Spring框架的ContextLoaderListener来加载Spring的配置文件
<?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"> <display-name>spring</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:com/gxa/spring/day02/spring-controller.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
4. 在Spring配置文件中使用<context:componet-scan .../>来扫描注解类
<?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:p="http://www.springframework.org/schema/p" 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 http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.gxa.spring.day02"></context:component-scan> </beans>
5. 创建UserServlet,通过覆盖HttpServlet中init方法来测试UserServlet是否被Spring框架所管理。因为@Autowired在Servlet中会失效,所以我们需要加入Spring框架中AutowireCapableBeanFactory来让@Autowired起作用。这一步非常重要
package com.gxa.spring.day02; import java.io.IOException; 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.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.stereotype.Controller; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; @Controller @WebServlet("/user.do") public class UserServlet extends HttpServlet { /** * Servlet和Spring框架整合时候, Servlet本身是不能自动依赖注入 * 解决方案: * 1. Spring框架提供的接口, AutowireCapableBeanFactory * 2. 就可以完成Servlet中依赖对象的自动装配 */ @Autowired private UserService userService; @Override public void init() throws ServletException { /** * 利用init方法来调用Spring容器BeanFactory * 看看UserServlet是否能够通过Spring容器获取对象 */ WebApplicationContext wc = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); //通过Web容器去得到BeanFactory对象 AutowireCapableBeanFactory autowireCapableBeanFactory = wc.getAutowireCapableBeanFactory(); autowireCapableBeanFactory.autowireBean(this); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { userService.getUser(); } }
6. 创建业务逻辑层接口和实现类
- 注意:由于这篇文章是对Spring整合Servlet的一个补充,我就没有进行MVC的分包。所以后面创建的业务层代码和Dao层都和Controller在同一个包里面
- 业务逻辑层实现类中要定义好注解的bean id名称,这样子Servlet通过@Autowired才能找到依赖的bean。不然会出现NoSuchDefinitionException
package com.gxa.spring.day02; public interface UserService { public void getUser(); }
package com.gxa.spring.day02; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service("userService") public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public void getUser() { userDao.getUser(); } }
7. 创建Dao层接口和它的实现类。因为想Dao层能够获取到数据库中数据,所以这里用了JdbcTemplate来搞定数据查询
<?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:p="http://www.springframework.org/schema/p" 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 http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.gxa.spring.day02"></context:component-scan> <context:property-placeholder location="classpath:com/gxa/spring/day02/jdbc.properties"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${mysql.driver}"></property> <property name="url" value="${mysql.url}"></property> <property name="username" value="${mysql.username}"></property> <property name="password" value="${mysql.password}"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
package com.gxa.spring.day02; public interface UserDao { public void getUser(); }
package com.gxa.spring.day02; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; @Repository("userDao") public class UserDaoImpl implements UserDao { @Autowired private JdbcTemplate jdbcTemplate; @Override public void getUser() { String sql = "select * from student"; List<Map<String,Object>> list = jdbcTemplate.queryForList(sql); System.out.println(list); } }