1. Table structure and data
2. Jar Package Used
3. Service, DAO layer interface and implementation class:
DAO Interface:
// transfer case persistence layer interface Public interface Accountdao { /* * * @param out: Transfer account * @param money: Transfer Amount */ public void Outmoney (String out ,double money); /* * * @param in: Transfer account * @param money : Transfer amount */ public void Inmoney (String in ,double Money); }
DAO implementation class:
//Transfer case Persistence layer implementation class Public classAccountdaoimpl extends Jdbcdaosupport implements Accountdao {/** * @param out: Transfer account * @param money: Transfer amount*/@Override Public voidOutmoney (String out, Double Money) {String SQL="Update account Set money = Money-? WHERE name =?"; This. Getjdbctemplate (). Update (Sql,money, out); } /** * @param in: Transfer account * @param money: Amount Transferred*/@Override Public voidInmoney (Stringinch, Double Money) {String SQL="Update account Set money = Money +? WHERE name =?"; This. Getjdbctemplate (). Update (Sql,money,inch); }}
Service Interface:
// transfer case Business layer interface Public Interface Accountservice { /* * * @param out: Transfer account * @param in : Transfer account * @param Money: Transfer amount */ public void -In , Double money);}
Service Implementation class:
Using @transactional things annotations, you can use different annotation attributes depending on your needs propagation, isolation, readOnly, rollbackfor, norollbackfor
//Transfer case Business Layer implementation class@Transactional (propagation=propagation.required)//Using annotations for transaction management, using different annotation attributes as required Public classAccountserviceimpl implements Accountservice {//the DAO injected into the transfer@ResourcePrivateAccountdao Accountdao; /** * @param out: Transfer account * @param in: Transfer account * @param money: Transfer amount*/@Override Public voidTransfer (String out, Stringinch, Double Money) { //put business operations into internal classes----in one thing (with success, same failure)Accountdao.outmoney ( out, Money); inti =1/0;//Anomaly TestingAccountdao.inmoney (inch, Money); }}
4. applicationcontext.xml File configuration:
<?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:AOP="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:context="Http://www.springframework.org/schema/context"Xmlns:tx="Http://www.springframework.org/schema/tx"xsi:schemalocation="Http://www.springframework.org/schema/beanshttp//www.springframework.org/schema/beans/spring-beans.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp//www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp//Www.springframework.org/schema/contexthttp//www.springframework.org/schema/context/spring-context-4.0.xsdhttp//Www.springframework.org/schema/txhttp//www.springframework.org/schema/tx/spring-tx-4.0.xsd "><!--introducing external properties Files---<context:property-placeholder location="classpath:jdbc.properties"/> <!--configuring C3P0 Connection Pool--<bean id="DataSource" class="Com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="Driverclass"Value="${jdbc.driverclass}"></property> <property name="Jdbcurl"Value="${jdbc.url}"></property> <property name="User"Value="${jdbc.username}"></property> <property name="Password"Value="${jdbc.password}"></property> </bean> <!--configuring Business Tier Class-<bean id="Accountservice" class="Cn.xl.spring.demo4.AccountServiceImpl"> </bean> <!--Configuring persistent layer class-<bean id="Accountdao" class="Cn.xl.spring.demo4.AccountDaoImpl"> <property name="DataSource" ref="DataSource"></property> </bean> <!--things Manager configuration-<bean id="TransactionManager" class="Org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="DataSource" ref="DataSource"></property> </bean> <!--opening annotations--<tx:annotation-driven transaction-manager="TransactionManager"/></beans>
5. Test class:
//Spring Declarative Way of managing things two test classes: ASPECTJ-based XML configuration@RunWith (Springjunit4classrunner.class) @ContextConfiguration ("Classpath:applicationContext4.xml") Public classSpringDemo4 {//need to inject business layer proxy objects@Resource (name="Accountservice") PrivateAccountservice Accountservice; @Test Public voidDemo4 () {Accountservice.transfer ("AAA","BBB", 200d); }}
Spring Things management-declarative (AspectJ) annotation implementations (recommended)