Transfer Case Environment Setup
1. Introduction of the JAR package
6 packages of the IOC
4 Packages of AOP
C3P0 's 1 Packs
MySQL's 1 drive packs
2 Target packages for JDBC
Integration of JUnit Test 1 packages
2. Introduction of configuration Files
Log4j.properties+applicationcontext.xml
### direct log messages to stdout # #log4j. Appender.stdout =org.apache.log4j.consoleappenderlog4j.appender.stdout.target = system.errlog4j.appender.stdout.layout = Org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern =%d{absolute}%5p%c{1}:%l-%m%n### Direct messages to File MyLog.log # # #log4j. Appender.file =org.apache.log4j.fileappenderlog4j.appender.file.file =c\: Mylog.loglog4j.appender.file.layout = Org.apache.log4j.PatternLayoutlog4j.appender.file.layout.ConversionPattern =%d{absolute}%5p%c{1}:%l-%m%n### set log levels-for more verbose logging change ' info ' to ' Debug ' # # #log4j. Rootlogg Er =info, stdout
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "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"Xmlns:tx= "Http://www.springframework.org/schema/tx"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 "> <!--c3p0 Connection Pool - <BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource"> < Propertyname= "Driverclass"value= "Com.mysql.jdbc.Driver"/> < Propertyname= "Jdbcurl"value= "Jdbc:mysql:///spring_day03"/> < Propertyname= "User"value= "root"/> < Propertyname= "Password"value= "Toor"/> </Bean> </Beans>
3. Create the corresponding package structure
Public InterfaceAccountdao {/*** turn out *@paramOut *@param Money*/ Public voidOutmoney (String out,DoubleMoney ); /*** Transfer to *@paramIn *@param Money*/ Public voidInmoney (String in,DoubleMoney );}
Public classAccountdaoimplextendsJdbcdaosupportImplementsAccountdao {//turn out@Override Public voidOutmoney (String out,DoubleMoney ) { //TODO auto-generated Method Stub This. Getjdbctemplate (). Update ("Update t_account set money=money-?"). where Name=? ", money,out); } //transferred@Override Public voidInmoney (String in,DoubleMoney ) { //TODO auto-generated Method Stub This. Getjdbctemplate (). Update ("Update t_account set money=money+?"). where Name=? ", Money,in); }}
Public Interface Accountservice { /** * transfer @param out@param in @param money*/public void Pay (String out,string in,double money ); }
Public classAccountserviceimplImplementsAccountservice {PrivateAccountdao Accountdao; Public voidSetaccountdao (Accountdao Accountdao) { This. Accountdao =Accountdao; } @Override Public voidPay (string out, string in,DoubleMoney ) { //TODO auto-generated Method Stub//turn outAccountdao.outmoney (out, money); //Exception//int i = 1/0; //transferredAccountdao.inmoney (in, money); }}
4. Modify the configuration file
<!--managing the business layer injection persistence layer objects - <BeanID= "Accountservice"class= "Com.spring.demo1.AccountServiceImpl"> < Propertyname= "Accountdao"ref= "Accountdao"/> </Bean> <!--Manage persistent Layer injection data sources (automatically generate JDBC template classes due to inheritance jdbcdaosupport) - <BeanID= "Accountdao"class= "Com.spring.demo1.AccountDaoImpl"> < Propertyname= "DataSource"ref= "DataSource"/> </Bean>
5. Testing
@RunWith (Springjunit4classrunner. Class) @ContextConfiguration (Value= "Classpath:applicationContext.xml") Public class Demo1 { @Resource (name= "Accountservice") private Accountservice Accountservice; @Test publicvoid m01 () { Accountservice.pay ("Test 1", "Test 2", 1000 ); }}
No abnormal normal transfer
An exception does not roll back
1.XML mode
1. Configure the transaction manager
<!-- configuration transaction manager --> < bean id = "TransactionManager" class = "Org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name = "DataSource" ref =" DataSource " /> </ bean >
2. Configuring Transaction Enhancements
<!--Configuring Transaction Enhancements - <Tx:adviceID= "Myadvice"Transaction-manager= "TransactionManager"> <tx:attributes> <Tx:methodname= "Pay"/> </tx:attributes> </Tx:advice>
Name= "Pay" represents the entry point
4. Configuring AOP Facets
<!--- <aop:config> < advice-ref= "Myadvice" pointcut= "Execution (* Com.spring.demo2.AccountServiceImpl.pay (..)) " /> </ Aop:config >
注意:如果是自己编写的切面,使用<aop:aspect>标签,如果是系统制作的,使用<aop:advisor>标签。
5. Testing
@RunWith (Springjunit4classrunner. Class) @ContextConfiguration (Value= "Classpath:applicationContext2.xml") Public class Demo1 { @Resource (name= "Accountservice") private Accountservice Accountservice; @Test publicvoid m01 () { Accountservice.pay ("Test 1", "Test 2", 1000 ); }}
No abnormal normal transfer
An exception rollback occurred
2. How to Annotate
1. Configure the transaction manager
<!--- < id = "TransactionManager" Class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager"> < name= "DataSource" ref= "DataSource"/> </ Bean >
2. Turn on annotation transactions
<!--- <Transaction-manager = " TransactionManager "/>
3. Add annotations
在业务层上添加一个注解:@Transactiona
4. Testing
No abnormal normal transfer
An exception rollback occurred
@RunWith (Springjunit4classrunner. Class) @ContextConfiguration (Value= "Classpath:applicationContext3.xml") Public class Demo1 { @Resource (name= "Accountservice") private Accountservice Accountservice; @Test publicvoid m01 () { Accountservice.pay ("Test 1", "Test 2", 1000 ); }}
10.Spring transaction Management "TX"