Spring+SpringJDBC+Servlet整合
一.步骤
1.在pom.xml加入SSS相应jar包
2.在resources目录下引入applicationContext.xml
3.在applicationContext.xml文件中配置
数据源
事务(AOP)
JDBCTemplate模板
IOC管理组件间依赖(service,dao)
4.Servlet组件创建将由WEB容器(获取spring管理的service组件)
5.Spring容器初始化(在web.xml文件中初始化)
二.实现
1.在pom.xml加入SSS相应jar包
< dependencies>
< dependency>
< groupId> aopalliance</ groupId>
< artifactId> aopalliance</ artifactId>
< version> 1.0</ version>
</ dependency>
< dependency>
< groupId> org.aspectj</ groupId>
< artifactId> aspectjweaver</ artifactId>
< version> 1.9.0</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-web</ artifactId>
< version> 5.1.3.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-aspects</ artifactId>
< version> 5.1.3.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-context</ artifactId>
< version> 5.1.3.Release</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-core</ artifactId>
< version> 5.1.3.Release</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-beans</ artifactId>
< version> 5.1.3.Release</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-context-support</ artifactId>
< version> 5.1.3.Release</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-expression</ artifactId>
< version> 5.1.3.Release</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-test</ artifactId>
< version> 5.1.3.Release</ version>
</ dependency>
< dependency>
< groupId> commons-logging</ groupId>
< artifactId> commons-logging</ artifactId>
< version> 1.1.2</ version>
</ dependency>
< dependency>
< groupId> log4j</ groupId>
< artifactId> log4j</ artifactId>
< version> 1.2.14</ version>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.12</ version>
</ dependency>
< dependency>
< groupId> org.springframework</ groupId>
< artifactId> spring-jdbc</ artifactId>
< version> 5.1.3.Release</ version>
</ dependency>
< dependency>
< groupId> com.alibaba</ groupId>
< artifactId> druid</ artifactId>
< version> 1.1.12</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.40</ version>
</ dependency>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> javax.servlet-api</ artifactId>
< version> 4.0.0</ version>
< scope> provided</ scope>
</ dependency>
< dependency>
< groupId> javax.servlet</ groupId>
< artifactId> jstl</ artifactId>
< version> 1.2</ version>
</ dependency>
</ dependencies>
2.applicationContext.xml文件配置
<?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: context= " http://www.springframework.org/schema/context"
xmlns: tx= " http://www.springframework.org/schema/tx"
xmlns: aop= " http://www.springframework.org/schema/aop"
xmlns: p= " http://www.springframework.org/schema/p"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd" >
< context: property-placeholder location = " classpath:db.properties" > </ context: property-placeholder>
< bean id = " dataSource" class = " com.alibaba.druid.pool.DruidDataSource" >
< property name = " driverClassName" value = " ${db_driver}" > </ property>
< property name = " url" value = " ${db_url}" > </ property>
< property name = " username" value = " ${db_username}" > </ property>
< property name = " password" value = " ${db_password}" > </ property>
</ bean>
< bean id = " jdbctemp" class = " org.springframework.jdbc.core.JdbcTemplate" >
< property name = " dataSource" ref = " dataSource" > </ property>
</ bean>
< bean id = " txManager" class = " org.springframework.jdbc.datasource.DataSourceTransactionManager" >
< property name = " dataSource" ref = " dataSource" > </ property>
</ bean>
< tx: annotation-driven transaction-manager = " txManager" > </ tx: annotation-driven>
< context: component-scan base-package = " service,dao" > </ context: component-scan>
</ beans>
3.Servlet中获取Spring上下文
public class UserListServlet extends HttpServlet {
private ApplicationContext applicationContext;
@Override
public void init ( ServletConfig config) throws ServletException {
super . init ( config) ;
applicationContext = WebApplicationContextUtils. getRequiredWebApplicationContext ( this . getServletContext ( ) ) ;
}
}
4.在web.xml初始化Spring容器
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
< web-app>
< display-name> Archetype Created Web Application</ display-name>
< 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>
</ web-app>