Spring AOP instances and Spring AOP instances
Detailed description of AOP instances in Spring
Services to be enhanced
If the following service is available, its function is very simple. Print the input parameters and return the parameters.
@Servicepublic class SimpleService { public String getName(String name) { System.out.println(get name is: + name); return name; }}
Define cut points and cut points
@ Component @ Aspectpublic class LogAspect {// defines the cut point @ Pointcut (within (com. ydoing. service .. *) // @ Pointcut (execution (* com. ydoing. service. *. *(..))) public void pointCut (){}}
Before Enhancement
// Define Before enhancement processing // execute enhancement processing Before calling the target method @ before (pointCut () public void Before (JoinPoint jp) {// obtain the input parameter of the connection point // Object args = jp. getArgs (); System. out. println (Before enhancement -- execute before target method call );}
Test output:
Before enhancement -- execute before target method callget name is: Bob
AfterReturning Enhancement
// Execute the enhancement processing @ AfterReturning (pointcut = pointCut (), returning = ret) public void afterReturning (JoinPoint jp, Object ret) {System. out. println (AfterReturnin enhanced processing -- execute after target method call, return value is: + ret );}
Test output:
Get name is: BobAfterReturnin enhanced processing -- execute after target method call, return value is: Bob
Around Enhancement
@ Around (pointCut () public void around (ProceedingJoinPoint jp) {System. out. println (Around enhancement -- around start ...); object [] args = jp. getArgs (); // modify the args [0] = around_add _ + args [0]; try {System. out. println (modify input parameters and execute output); jp. proceed (args);} catch (Throwable e) {// TODO Auto-generated catch block e. printStackTrace ();} System. out. println (Around enhancement -- around end );}
Output:
Around enhancement -- around start... modify the input parameters and run the following command: get name is: around_add_BobAround enhancement -- around end
After enhancement
// Process @ After (pointCut () public void after () {System. out. println (After enhancement -- always do no matter what happen) regardless of whether an exception occurs );}
Output:
Get name is: BobAfter enhancement -- always do no matter what happen
AfterThrowing Enhancement
@AfterThrowing(pointcut = pointCut(), throwing = ex) public void afterThrowing(JoinPoint jp, Throwable ex) { System.out.println(error is: + ex); }
No exception is thrown here, so no output is generated.
The test code is as follows:
@Configuration@EnableAspectJAutoProxy@ComponentScan(basePackages = com.ydoing.service,com.ydoing.aspect)public class AppConfig { public static void main(String[] args) { @SuppressWarnings(resource) ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); SimpleService service = ctx.getBean(SimpleService.class); service.getName(Bob); }}
Address: http://www.bkjia.com/Javabc/1077751.html
QQ Group 290551701 has gathered many Internet elites, Technical Directors, architects, and project managers! Open-source technology research, welcome to the industry, Daniel and beginners interested in IT industry personnel!