Spring整合web
来源:互联网 发布:同意的计算 pdf百度云 编辑:程序博客网 时间:2024/06/06 13:14
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,收款是Receiptpublic 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) */@Overridepublic void pay(String outer, Integer money) {// TODO Auto-generated method stubthis.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) */@Overridepublic void receipt(String inner, Integer money) {// TODO Auto-generated method stubthis.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;}@Overridepublic void transfer(String outer, String inner, Integer money) {// TODO Auto-generated method stubaccountDao.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 {@Testpublic 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);}}
- spring---整合Junit,整合web
- Spring-整合Web
- spring整合web
- Spring整合web项目
- spring整合web项目
- Spring整合web
- spring整合web项目
- Spring与Web整合
- Spring整合web项目
- spring整合web
- Spring整合web项目原理
- Spring 整合web项目原理
- spring整合web详细介绍
- Spring整合web项目原理
- Spring整合Web,Mybatis,Hibernate
- Spring如何整合WEB框架
- Spring整合Web项目原理
- Spring整合Web项目原理
- 交叉编译samba服务器
- 美团codeM资格赛 优惠券
- MFC 文件分块复制
- Trie树的持久化应用
- Java WebService 简单实例
- Spring整合web
- 【怎样写代码】向现有类型“添加”方法 -- 扩展方法(三):为枚举类型创建方法
- mysql 实用操作表属性 数据库属性 等
- 【转】WPF MVVM 循序渐进 (从基础到高级) 【已翻译100%】【2】
- 谷歌发布第二代TPU,并提供了免费试用方案
- 微信小程序里面的单步调试和变量查看
- 拟合
- A New GOP Level bit Allocation Method for HEVC Rate Control
- Exynos4412 Uboot 移植(三)—— Uboot添加自定义命令