Spring annotation-based configuration AOP

  1. Prepare the environment

the pom.xml 
<-! pointcut expression configuration parsing a jar ->
<dependency>
<the groupId> org.aspectj </ the groupId>
<the artifactId> aspectjweaver </ the artifactId>
<Version> 1.8.7 </ Version>
</ dependency>

Spring profile
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"

http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd

<! - scan path configuration -> 
<context: Component Base-Package-Scan = "com.aiitec"> </ context: Component-Scan>
<! - Configure Spring AOP's open support for annotations, if you can not write notes in the enhanced use @EnableAspectJAutoProxy class to turn this feature ->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
2. Enhance class configuration notes
org.aspectj.lang.ProceedingJoinPoint Import; 
Import org.aspectj.lang.annotation *;.
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.context.annotation.EnableAspectJAutoProxy;
Import org.springframework.stereotype .component;

Import java.sql.SQLException;
@Component ( "transactionManager")
@ Aspect // represents the current class is a class section
// @EnableAspectJAutoProxy configure Spring AOP is open to support notes
the TransactionManager class {public 

// define a pointcut expression (must bring referencing '()')
@Pointcut ( "Execution (com.aiitec.service.impl *. *. * (..))")
Private void PointCut () {}

@Autowired
Private connectionUtils connectionUtils;


// the Before @ ( "PointCut ()")
public void beginTransaction () {
the try {
connectionUtils.getConnection () the setAutoCommit (to false);.
System.out.println ( "pre notification start ... ");
} the catch (SQLException E) {
e.printStackTrace ();
}
}

// @AfterReturning (" PointCut () ")
public void the commit () {
the try {
connectionUtils.getConnection () the commit ();.
System.out.println ( "post-notification start ...");
} the catch (SQLException E) {
e.printStackTrace ();
}
}
// @ AfterThrowing ( "PointCut ( ) ")
public void ROLLBACK () {
the try {
connectionUtils.getConnection () ROLLBACK ();.
System.out.println (" abnormality notification start ... ");
} the catch (SQLException E) {
e.printStackTrace ();
}
}
// the After @ ( "PointCut ()")
public void Relese () {
the try {
// is not disconnected at this time, but also back to the connection pool
connectionUtils.getConnection () close ().;
connectionUtils.removeConnection ();
System.out.println ( "final notification start ...");
} the catch (SQLException E) {
e.printStackTrace ();
}
}
@Around ( "PointCut ()")
public Object arundAdvice ( PJP ProceedingJoinPoint) {
Object Result = null;
the try {
Object [] args = pjp.getArgs ();
beginTransaction (); // pre-notification
result = pjp.proceed (args); // perform certain method
commit (); / / post-notification
} the catch (Throwable the Throwable) {
ROLLBACK (); // abnormality notification
Throwable.printStackTrace ();
} {the finally
Relese (); // a final notification
}
return result;
}
}
Note: Use annotations recommended way to use AOP around advice , because the spring there is a bug, if you do not use around advice, before the final notification will post notification and exception notifications to interrupt the business logic (When are repaired, also please tell me, I modify, thank you).

Guess you like

Origin www.cnblogs.com/zou-rong/p/12002832.html