Spring整合Web
Spring整合web的原理是让Spring容器随着tomcat容器ServletContext的启动而启动。并且在初始化完成后放到整个应用都可以访问的范围。
具体而言,tomcat启动加载配置文件,首先要init servlet,然后 init filter,web.xml注册过滤器自动调用初始化。Spring提供监听器ContextLoaderListener,确定配置文件的位置,通过系统初始化参数,完成启动。
步骤:
1、导入Jar包
spring-web-3.2.0.RELEASE.jar
2、编写DAO接口
public interface AccountDAO {
//汇款是pay,收款是Receipt
public void pay(String outer,Integer money);
public void receipt(String inner,Integer money);
}
3、编写DAO实现类
public class AccountDAOImpl extends JdbcDaoSupport implements AccountDAO {
/* (non-Javadoc)
* @see com.Lily.SpringLearning.J_tx_xml.AccountDAO#pay(java.lang.String, java.lang.Integer)
*/
@Override
public void pay(String outer, Integer money) {
// TODO Auto-generated method stub
this.getJdbcTemplate().update("update account set money = money - ? where username = ?", money,outer);
}
/* (non-Javadoc)
* @see com.Lily.SpringLearning.J_tx_xml.AccountDAO#receipt(java.lang.String, java.lang.Integer)
*/
@Override
public void receipt(String inner, Integer money) {
// TODO Auto-generated method stub
this.getJdbcTemplate().update("update account set money = money + ? where username = ?", money,inner);
}
}
4、编写service接口
public interface Ser {
//转账服务
public void transfer(String outer,String inner,Integer money);
}
5、编写service实现类
public class AccountSerImpl implements Ser {
/* (non-Javadoc)
* @see com.Lily.SpringLearning.J_tx_xml.Ser#transfer(java.lang.String, java.lang.String, java.lang.Integer)
*/
private AccountDAO accountDao;
public AccountDAO getAccountDao() {
return accountDao;
}
public void setAccountDao(AccountDAO accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfer(String outer, String inner, Integer money) {
// TODO Auto-generated method stub
accountDao.pay(outer, money);
//模拟故障
//int i=1/0;
accountDao.receipt(inner, money);
}
}
6、编写servlet实现类
这里有两种从application作用域获得Spring容器的方式
方式一:手动从作用域获得
ApplicationContext applicationContext =ApplicationContext)this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
方式二:通过工具获得
ApplicationContextapppApplicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
public class LilyServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ApplicationContext at=WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
Ser service=(Ser) at.getBean("accountService");
service.transfer("Lily", "dianer", 5);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
7、编写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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<context:property-placeholder location="classpath:com/Lily/SpringLearning/I_c3p0/JdbcInfo.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean id="accountDao" class="com.Lily.SpringLearning.L_web.AccountDAOImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="accountService" class="com.Lily.SpringLearning.L_web.AccountSerImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
</beans>
8、编写测试类
public class txTest {
@Test
public void test() {
String xmlPath = "com/Lily/SpringLearning/L_web/applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
Ser accountService = (Ser) applicationContext.getBean("accountService");
accountService.transfer("dianer", "Lily", 6);
}
}