A detailed example of AOP in spring

Source: Internet
Author: User
Tags throwable

A detailed example of AOP in spring

Services that need to be enhanced

If you have the following service, his function is 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;    
Defining facets and tangency points
@Component@Aspectpublic class LogAspect {    // 定义切点    @Pointcut(within(com.ydoing.service..*))    // @Pointcut(execution(* com.ydoing.service.*.*(..)))    public void pointCut() {    } }
Before enhanced processing
    // 定义Before增强处理    // 在目标方法调用之前执行增强处理    @Before(pointCut())    public void before(JoinPoint jp) {        // 获取连接点传入参数        // Object args = jp.getArgs();        System.out.println(Before增强处理--execute before target method call);    

Test output:

Before增强处理--execute before target method callget name is: Bob
afterreturning Enhancement
    // 在目标方法调用之后执行增强处理    @AfterReturning(pointcut = pointCut(), returning = ret)    public void afterReturning(JoinPoint jp, Object ret) {        System.out.println(AfterReturnin增强处理--execute after target method call, return value is : + ret);    }

Test output:

get name is: BobAfterReturnin增强处理--execute after target method call, return value is :Bob
Around enhancement
    @Around(pointCut())    public void around(ProceedingJoinPoint jp) {        System.out.println(Around增强--around start...);        Object[] args = jp.getArgs();        // 修改目标方法传入的参数        args[0] = around_add_ + args[0];        try {            System.out.println(修改传入参数后执行输出:);            jp.proceed(args);        } catch (Throwable e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        System.out.println(Around增强--around end);    }

Output:

Around增强--around start...修改传入参数后执行输出:get name is: around_add_BobAround增强--around end
After enhancements
    // 无论是否发生异常都会 处理    @After(pointCut())    public void after() {        System.out.println(After增强--always do no matter what happen);    }

Output:

get name is: BobAfter增强--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);    }

There's no exception here, there's no output.

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);    }}

Original address: http://www.bkjia.com/Javabc/1077751.html

QQ group 290551701 gathers a lot of Internet elite, technical director, architect, project Manager! Open source technology research, Welcome to the industry, Daniel and beginners are interested in engaging in IT industry personnel to enter!

A detailed example of AOP in spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.