Use annotations to implement transaction (declarative transaction)
target: the transaction by the following method either all succeed, or all three fail
-
jar package
Spring -tx
-4.3.9.RELEASE transaction
ojdbc.jar
Commons-dbcp.jar source connected to a data pool using
commons-pool.jar connection pool
Spring-JDBC-4.3.9.RELEASE.jar
aopalliance.jar -
Configuring
jdbc \ mybatis \ spring
increase transaction tx namespace
<!-- 增加对事务的支持 -->
<tx:annotation-driven transaction-manager="txManager" />
- use
The former will have to be increased transaction method notes:
the @Transactional (readOnly = false, propagation = Propagation.REQUIRED)
AOP: Aspect-Oriented Programming
Into a normal class specific functional classes
a. Inherited class b. Implement interface c. Annotation d. Configure
public class MyFilter exntends/implements Xx
{
}
Class into a "notice": implement an interface
Pre-notification implementation steps:
-
jar
aopaliance.jar
aspectjweaver.jar -
Configuration
-
Prepared
aop: before automatically performed every time before add () method a log ();
addStudent (); business method (addStudent IStudentService.java in ())
before (); notification is automatically performed, i.e., pre-notification aop
public class Xxx
{
@Test
a(){}
}
If an exception occurs: similar java.lang.NoClassDefFoundError: org / apache / commons / pool / impl / GenericObjectPool
it indicates a lack of jar
After returning advice:
- Notification class, ordinary implements the interface
- Traffic class, business methods
StudentServiceImpl in addStudent () - Configuration:
The traffic class, springIOC notification into the container
defined entry point (end), the definition of class notification (the other end), by a pointcut-ref connecting ends
Exception notification:
By definition can be found abnormal notification interface, implementation class exception notification must write the following methods:
public void afterThrowing([Method, args, target], ThrowableSubclass)
public void afterThrowing(Method, args, target, ThrowableSubclass)
public void afterThrowing( ThrowableSubclass)
Around advice: before and after the target method, when an exception occurs, the final notice, etc. can be carried out in various places, the most powerful of a notification;
you can get full control of the target method (whether the target method, prior to execution, after execution, parameters, return values, etc.)
- When using around advice, all the information are available through the target method
invocation
obtains parameters - Notification surround the bottom by the interceptor achieved.
Implementation Notes implement the notification, aop
-
jar
and the way to achieve the same interface -
Configuring
the business class, the notification included springIOC container
Open comments AOP support for<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
business class addStudent - notice -
write
Notice:
@Aspect //声明该类 是一个 通知
public class LogBeforeAnnotation {
}
Note: The comment form is increased to ioc container, you need to set the scanner objects
<context:component-scan base-package="org.lanqiao.aop"></context:component-scan>
-
The scanner package will specify
@Componet
@Service
@Respository
@Controller
an object class to generate a modified IOC container is increased
@Aspect
without the addition of a scanner, can only turn:<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
-
Achieved through the notes in the form of aop, if you want to get some parameters of the target object, you need to use an object:
JointPoint
Notes in the form of the return value:
- Declare the name of the return parameter values:
@AfterReturning( pointcut= "execution(public * addStudent(..))" ,returning="returningValue" )
public void myAfter(JoinPoint jp,Object returningValue) {//returningValue是返回值,但需要告诉spring
System.out.println("返回值:"+returningValue );
-
When the comment form to achieve aop, parametric methods of notification can not be more than, less
-
Implement the interface forms, annotations, form capture only a statement of specific types of exceptions, while other types of abnormalities are not captured.
cath ()
Configuration change notification by the class
-based Schema
configuration
similar manner to implement the interface
- Interface notice: public class LogAfter implements AfterReturningAdvice
- Schema notify:
. A preparation of a general classpublic class LogAfter {}
b configure the class, into a "notice."
If you want to get the target object information:
notes, schema: JoinPoint
Interface: Method method, Object [] args , Object target
- schema annotations and form similar form, except: use annotations in the form of a registered @After, schmema form of a redundant configuration