web填坑-AOP

AOP面向切面编程

AOP
AOP 横切逻辑
AOP术语

利用Spring注解方式实现AOP功能

注解实现AOP功能

student.java

package com.jike.spring.chapter09.aop.aspect;

public class Student {
            public String print(String name){  
                    System.out.println("print() method:" + name);  
                    return "hello";  
                }  

}

StuInterceptor.java

package com.jike.spring.chapter09.aop.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class StuInterceptor {

    /**
     * 打印方法AOP
     */
    @Pointcut("execution(* com.jike.spring.chapter09.aop.aspect.Student.print(..))")
    // @Pointcut("execution(* com.jike.spring.chapter09.aop.aspect.Student.*(..))")
    public void printMethod() {
    }

    @Before("printMethod()")
    public void printBeforeAdvice() {
        System.out.println("printBeforeAdvice()!");
    }

    @AfterReturning(pointcut = "printMethod()", returning = "flag")
    public void printAfterAdvice(String flag) {
        System.out.println("printAfterAdvice()! " + flag);
    }

    @After("printMethod()")
    public void finallyAdvice() {
        System.out.println("finallyAdvice()!");
    }

    @Around("printMethod() && args(name)")
    public Object printAroundAdvice(ProceedingJoinPoint pjp, String name)
            throws Throwable {
        Object result = null;
        if (name.equals("whc"))
            pjp.proceed();
        else
            System.out.println("print()方法以及被拦截...");
        return result;
    }
}

  • @Pointcut指出了切入点就是print方法,限免的注释,使用通配符指出切入点为所有的Student类中的方法,第一个星号*,表示返回值可以为任意类型,第二个为方法通配符,括号中的两个点,表示的是任意参数

  • @Before定义了一个前置通知,即在printMethod前运行。

  • @AferReturning定义了一个后置通知,后面的returning表示获取方法的返回值,并放入flag变量中,和后置方法中的参数对应。

  • @After最终通知。

  • @Around环绕通知,方法中又一个ProceedingJoinPoint pjp,在下面的判断语句中,表示如果符合条件,则pjp.proceed().表示程序继续执行,若不符合,则不运行pjp.proceed(),程序不能继续执行。

conf-aspect.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"
    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-3.1.xsd">
    <aop:aspectj-autoproxy />
    <bean id="stuInterceptor" class="com.jike.spring.chapter09.aop.aspect.StuInterceptor"></bean>
    <bean id="stu" class="com.jike.spring.chapter09.aop.aspect.Student"></bean>
</beans>    

这里的<aop:aspectj-autoproxy />配置了aop切面代理的自动加载。

main.java入口类

package com.jike.spring.chapter09.aop.aspect;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
     public static void main(String[] args) {  
               ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/conf-aspect.xml");  
                 Student stu = (Student)ctx.getBean("stu");  
                    stu.print("whc");  
                }  

}

打印结果
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容