Best to use around advice written when using annotations write aop
Section categories:
/ ** * for logging tools inside it provides a common code * / @Component ( "Logger" ) @Aspect // represents a slice of the current class is class public class Logger { @Pointcut ( "Execution (* * * cn.flypig666.service.impl (..)).. " ) Private void PT1 () {}; / ** * pre-notification * / @Before ( " PT1 () " ) public void beforePrintLog () { System.out.println ( "pre-notification method beforePrintLog Logger class logging the beginning ...." ); } / ** * rear notification * / @AfterReturning ( "PT1 ()") Public void afterReturningPrintLog () { System.out.println ( "post-notification method afterReturningPrintLog Logger class logging the beginning ...." ); } / ** * abnormality notification * / the @AfterThrowing ( "PT1 () " ) public void afterThrowingPrintLog () { System.out.println ( " abnormality notification method afterThrowingPrintLog Logger class logging the beginning .... " ); } / ** * final notification * / @After ( " PT1 () " ) public void afterPrintLog () { System.out.println ( " final method notification afterPrintLog Logger class logging the beginning .... "); } / ** * Surround notification: * When we arranged surround notification, pointcut method is not performed, and the notification method executed, * Analysis: * Surround notice code by dynamic contrast agent found around the dynamic proxies notice a clear entry point method calls, but we did not code * resolved: * the Spring framework provides an interface, ProceedingJoinPoint for us, this interface has a method proceed (), this method is equivalent to explicitly call the entry point method * the interface can be notified as a parameter surrounded, at run time, provides us with a spring framework implementation class for our use of the interface * * in spring around advice: * it is a spring may provide the framework for our code when the manual enhancement mode control method executed * / @Around ( "PT1 ()" ) public Object aroundPrintLog (ProceedingJoinPoint PJP) { Object rtValue = null ; the try { Object [] args {Pjp.getArgs = (); // get perform the required method parameters System.out.println ( "Method aroundPrintLog Logger class begins logging the front ...." ); pjp.proceed (args); / / explicitly call service layer method (method pointcut) System.out.println ( "method aroundPrintLog Logger class begins logging the rear ...." ); return rtValue; } the catch (Throwable the Throwable) { the System.out .println ( "method aroundPrintLog Logger class begins logging the exception ...." ); the throw new new a RuntimeException (Throwable); } the finally System.out.println ("AroundPrintLog Method Logger class begins logging the final ...." ); } } }
bean.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
HTTP: // www.springframework.org/schema/context/spring-context.xsd ">
<- Configuration spring when creating a container to be scanned bag ->!
<context: the Component base- -scan Package Penalty for = "cn.flypig666"> </ context: the Component-Scan>
! <- configuration spring open comments AOP support ->
<AOP: AspectJ-autoproxy> </ AOP: AspectJ-autoproxy>
< / beans>
Use may be omitted @EnableAspectJAutoProxy <aop: aspectj-autoproxy> < / aop: aspectj-autoproxy>
AOP expressed open support for annotations, do not write in springboot, it has been turned on by default