AOP面向切面编程

AOP面向切面编程

aop:面向切面编程,采取的是横向抽取机制,取代了传统的纵向继承体系重复性的代码,简单的来说就是拓展功能的时候可以不通过修改源码实现。

举个例子,在最原始的机制中,当我们要给一个类中的方法添加某个功能的时候,比如为了后期维护系统方便,要给方法添加日志打印的功能,这个时候只能通过修改源代码的方式实现,当系统中有很多类的时候,修改源代码就显得费时费力了。

这个时候可以采用纵向的抽取机制,比如将所有的需要更改功能的类都继承一个父类,在子类中调用父类的方法,这样就少写很多代码。然而,这样的机制仍然不是最方便的,如果父类中的方法发生改变,那么调用父类方法的所有子类都要修改。

1.AOP的原理

aop采用的是横向抽取机制,其底层使用动态代理方式实现。在采用动态代理方式时,针对有接口和没有接口的情况,分别采用不同的方式实现。

1.针对有接口的情况,使用的是jdk的动态代理。实现类在实现接口的中间,创建接口实现类的代理对象,这个代理对象和实现类是平级的,并不是一个真正的对象,只是实现和接口实现类相同的功能。

2.针对没有接口的情况,使用的是cglib的动态代理,创建该类的子类代理对象,在子类中调用父类的方法完成增强。

2.AOP操作术语

class UserService{

    void add(User user){}

    void update(User user){}

    void delete(Integer userId){}

    User getUserById(Integer userId){}
}

Joinpoint(连接点):在一个类中那些可以被增强的方法,这些方法称之为连接点,比如UserService中的四个方法,都可以称之为连接点,

Pointcut(切入点):在一个类中可以有多个连接点,但是只有实际被增强的方法才称之为切入点。如果只对UserService中的add()方法进行增强,那么这个类中只有一个切入点。

Advice(通知/增强):增强的逻辑,比如说要给add()方法增加日志打印的功能,那么这个功能就称之为通知/增强。有前置通知、后置通知、异常通知、最终通知、环绕通知。

Aspect(切面):把增强应用到具体方法上面的过程。比如说给add()方法增加日志打印的这个过程,就是切面。

3.Spring中的aop操作@Aspectj(基于xml)

//被增强的类
public class BookService {

    public void add() {
        System.out.println("BookService.add()");
    }
}
public class BookServiceAdvice {

    //前置通知
    public void before() {
        System.out.println("before advice");
    }

    //后置通知
    public void after() {
        System.out.println("after advice");
    }

    //环绕通知
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //在方法之前执行的方法
        System.out.println("before method advice");

        proceedingJoinPoint.proceed();

        //在方法之后执行的方法
        System.out.println("after method advice");
    }
}

1.导入jar包
- aopallinace-1.0.jar
- aspectjweaver-1.8.7.jar
- spring-aop-4.2.4.RELEASE.jar
- spring-aspects-4.2.4.RELEASE.jar
或者添加pom依赖:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aspects</artifactId>
  <version>4.2.4.RELEASE</version>
</dependency>

2.在Spring配置文件中导入约束

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

3.几种常见的常用的表达式

execution(<访问修饰符>?<返回类型><方法名>(<参数>)<异常>)

  • execution(* com.aop.BookService.add(..))  *表示匹配所有的方法修饰符,..表示参数

  • execution(* com.aop.BookService.*(..))  第一个*表示匹配所有的方法修饰符,第二个*表示匹配该类下的所有方法

  • execution(* *.*(..)) 表示对所有的方法进行增强

  • execution(* add*(..)) 表示对所有的以add开头的方法进行增强

4.配置对象(基于xml)

<bean id="bookService" class="com.aop.BookService"></bean>
<bean id="bookServiceAdvice" class="com.aop.BookServiceAdvice"></bean>

5.配置aop操作

<aop:config>
    <!--配置切入点-->
    <aop:pointcut expression="execution(* com.aop.BookService.add(..))" id="bookService_add_pointcut"/>
    <!--配置切面-->
    <aop:aspect ref="bookServiceAdvice">
        <!--配置增强类型-->
        <!--前置通知-->
        <aop:before method="before" pointcut-ref="bookService_add_pointcut"/>
        <!--后置通知-->
        <aop:after-returning method="after" pointcut-ref="bookService_add_pointcut"/>
        <!--环绕通知-->
        <aop:around method="around" pointcut-ref="bookService_add_pointcut"/>
    </aop:aspect>
</aop:config>

6.测试

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
BookService bookService = (BookService) context.getBean("bookService");
bookService.add();

运行结果:

before advice
before method advice
BookService.add()
after method advice
after advice

4.Spring中的aop操作@Aspectj(基于注解)

在类上面加注解,导入jar包或者添加依赖,在配置文件中导入约束

@Service("bookService")
public class BookService {...}
@Service("bookServiceAdvice")
public class BookServiceAdvice {...}

1.在配置文件中开启aop操作

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

2.在增强类上面添加注解

@Service("bookServiceAdvice")
@Aspect
public class BookServiceAdvice {

    //前置通知
    @Before(value = "execution(* com.aop.BookService.add(..))")
    public void before() {
        System.out.println("before advice");
    }

    //后置通知
    @After(value = "execution(* com.aop.BookService.add(..))")
    public void after() {
        System.out.println("after advice");
    }

    //环绕通知
    @Around(value = "execution(* com.aop.BookService.add(..))")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //在方法之前执行的方法
        System.out.println("before method advice");

        proceedingJoinPoint.proceed();

        //在方法之后执行的方法
        System.out.println("after method advice");
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值