Spring基本应用配置
2016-12-05 00:00
513 查看
摘要: Spring基本应用配置
1.资源配置文件 db.properties
2.applicationContext.xml
3.BookShopDao.java
BookShopImpl.java
BookShopService.java
BookShopServiceImpl.java
Cashier.java
CashierImpl.java
SpringTransactionTest.java
1.资源配置文件 db.properties
jdbc.user=root jdbc.password=linuxG jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql:///spring4 jdbc.initialPoolSize=5 jdbc.maxPoolSize=10
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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <context:component-scan base-package="com.huangliusong.spring"></context:component-scan> <!-- 导入资源文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 配置C3P0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean> <!-- 配置 Spring的jdbcTemplate --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <constructor-arg ref="dataSource"></constructor-arg> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 启用事务管理器 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
3.BookShopDao.java
package com.huangliusong.spring.tx.xml; public interface BookShopDao { //根据书号获取价格 public int findBookPriceByIsBn(String isbn); //更新书的库存 使得书号 对应的库存-1 public void updateBookStock(String isbn); //更新用户的余额 使得username的balance-price public void updateUserAcount(String username,int price); }
BookShopImpl.java
package com.huangliusong.spring.tx.xml; import org.springframework.jdbc.core.JdbcTemplate; import com.huangliusong.spring.tx.exception.BookStockException; import com.huangliusong.spring.tx.exception.UserAcountException; public class BookShopImpl implements BookShopDao { private JdbcTemplate jdbcTemplate; public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public BookShopImpl() { } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } @Override public int findBookPriceByIsBn(String isbn) { String sql = "select stock from book_stock where isbn=?"; return jdbcTemplate.queryForObject(sql, Integer.class, isbn); } public BookShopImpl(JdbcTemplate jdbcTemplate) { super(); this.jdbcTemplate = jdbcTemplate; } @Override public void updateBookStock(String isbn) { // 检测书库中的库存是否足够 如果不够则抛出异常\ String sql2 = "select stock from book_stock where isbn=?"; int stock = jdbcTemplate.queryForObject(sql2, Integer.class, isbn); if (stock == 0) { // 抛出异常 throw new BookStockException("库存不足"); } String sql = "update book_stock set stock=stock-1 where isbn=?"; jdbcTemplate.update(sql, isbn); } @Override public void updateUserAcount(String username, int price) { //验证余额是否足够 若不足 则跑出异常 String sql2 = "select balance from acount where username=?"; int balance = jdbcTemplate.queryForObject(sql2, Integer.class, username); if (balance == 0) { // 抛出异常 throw new UserAcountException("余额不足"); } String sql = "update acount set balance=balance-? where username=?"; jdbcTemplate.update(sql, price, username); } }
BookShopService.java
package com.huangliusong.spring.tx.xml; public interface BookShopService { public void purchase(String username, String isbn); }
BookShopServiceImpl.java
package com.huangliusong.spring.tx.xml; public class BookShopServiceImpl implements BookShopService { private BookShopDao bookShopDao; public BookShopDao getBookShopDao() { return bookShopDao; } public void setBookShopDao(BookShopDao bookShopDao) { this.bookShopDao = bookShopDao; } //添加事务注解 @Override public void purchase(String username, String isbn) { // 1获取书的单价 int price=bookShopDao.findBookPriceByIsBn(isbn); // 2更新库存 bookShopDao.updateBookStock(isbn); // 3更新用户的余额 bookShopDao.updateUserAcount(username, price); } }
Cashier.java
package com.huangliusong.spring.tx.xml; import java.util.List; public interface Cashier { public void checkOut(String username,List<String> isbn); }
CashierImpl.java
package com.huangliusong.spring.tx.xml; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.huangliusong.spring.tx.exception.UserAcountException; public class CashierImpl implements Cashier { private BookShopService bookShopService; @Override public void checkOut(String username, List<String> isbns) { for (String isbn : isbns) { bookShopService.purchase(username, isbn); } } public BookShopService getBookShopService() { return bookShopService; } public void setBookShopService(BookShopService bookShopService) { this.bookShopService = bookShopService; } }
SpringTransactionTest.java
package com.huangliusong.spring.tx.xml; import java.util.Arrays; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTransactionTest { private ApplicationContext ctx=null; private BookShopDao bookShopDao=null; private BookShopService bookShopService=null; private Cashier cashier=null; { ctx=new ClassPathXmlApplicationContext("applicationContext-xmls.xml"); bookShopDao=(BookShopDao)ctx.getBean(BookShopDao.class); bookShopService=(BookShopService)ctx.getBean(BookShopService.class); cashier=(Cashier)ctx.getBean(Cashier.class); } @Test public void testBookShopService(){ bookShopService.purchase("hls", "1"); } @Test public void testBookShopTransactional(){ cashier.checkOut("hls", Arrays.asList("1","2")); } }
相关文章推荐
- Spring4 的知识应用总结(一)——基本配置jar包和设置
- SpringCloud系列九:SpringCloudConfig 基础配置(SpringCloudConfig 的基本概念、配置 SpringCloudConfig 服务端、抓取配置文件信息、客户端使用 SpringCloudConfig 进行配置、单仓库目录匹配、应用仓库自动选择、仓库匹配模式)
- Spring Boot应用基本配置说明
- Spring4.X + Spring MVC + Mybatis3 零配置应用开发框架搭建详解 (3) - 实现最基本的登录处理
- Spring 联合Hibernate ,Struts开发之基本配置
- rhel5之postfix邮件服务器基本应用配置 推荐
- Eclipse下配置spring应用
- Spring数据源的灵活配置巧应用
- spring2.0配置中的新标签应用
- Spring数据源的灵活配置巧应用
- Spring 2.5.1 MVC + Tiles 2.0.5 基本配置
- Eclipse下配置spring应用
- Spring quartz 应用配置
- Spring2.0配置中的新标签应用
- spring2.5 基本配置
- dwr基本应用2--creator="Spring"
- spring2.0配置中的新标签应用
- Eclipse下配置spring应用
- Spring中bean的基本xml配置
- java开源框架应用技巧之spring配置文件中如果有多个.hbm.xml文件的话,无论是项目开发过程中还是维护过程中修改起来都会很麻烦切容易出错