A, Spring AOP annotation-based configuration
1. Suppose you create a need to enhance AccountService (wherein each method will perform a method of adding logging), and then create a log of the class that implements the method of logging;
// the class injection spring containers @Component ( "Logger" ) @Aspect // represents a slice of the current class is class public class Logger { @Pointcut ( "Execution (com.li.service.impl *. *. * (. .)) " ) Private void PT1 () { } / ** * pre-notification * / @Before ( " PT1 () " ) public void beforePrintLog () { System.out.println ( " pre-notification class Logger the method begins logging beforePrintLog ... " ); } / ** * rear notification * / @AfterReturning ( " PT1 () " ) public void afterReturningPrintLog () { System.out.println ( "post-notification method afterReturningPrintLog Logger class begins logging ..." ); } / ** * abnormality notification * / the @AfterThrowing ( "PT1 ()" ) public void afterThrowingPrintLog () { System.out.println ( "abnormality notification method afterThrowingPrintLog Logger class begins logging ..." ); } / ** * final notification * / @After ( "PT1 ()" ) public void afterPrintLog ( ) { System.out.println ( "final method notification afterPrintLog Logger class begins logging ..." ); } /** * Surround notice * Question: * When we configured around advice, the entry point method is not performed, but the implementation of the notification method * Analysis: * Around advice dynamic proxies have a clear entry point method call, and we did not * resolved : * the Spring provides an interface: ProceedingJoinPoint, proceed to the interface () method is equivalent to an explicit call to the entry point method * the interface can be used as a method parameter around advice, during program execution, spring framework provides an interface for implementation classes for us we use * sprig around notification * can be enhanced when the manual control method performed in the code * / // @Around ( "PT1 ()") public Object aroundPrintLog (ProceedingJoinPoint PJP) { Object rtValue = null ; the try { Object [] args = pjp.getArgs (); System.out.println ("AroundPrintLog method Logger class pre-start logging ..." ); rtValue = pjp.proceed (args); // explicit call to have a business layer method (entry point method) System.out.println ( "Logger class the method begins logging aroundPrintLog post ... " ); return rtValue; } the catch (the Throwable T) { System.out.println ( " method aroundPrintLog Logger class begins logging exception ... " ); the throw new new a RuntimeException (T); } the finally { System.out.println ( "method aroundPrintLog Logger class begins final logging ..." ); } } }
@Service ( "AccountService" ) public class AccountServiceImpl the implements IAccountService { public void saveAccount () { System.out.println ( "a save" ); } public void updateAccount ( int I) { System.out.println ( "performed update "+ I); } public int deleteAccount () { System.out.println ( " delete " ); return 0 ; } }
2. Configure Open comments
<-! When you configure spring to create a container to be scanned package -> < context: the Component-Scan Base-Package Penalty for = "com.li" > </ context: the Component-Scan > <! - Configure spring Open comments AOP support -> < AOP: AspectJ-the autoproxy > </ AOP: AspectJ-the autoproxy >