Spring AOP(面向切面编程)

2018-08-22  本文已影响26人  PC_Repair

AOP 即 Aspect Oriented Program 面向切面编程。首先,在面向切面编程的思想里面,把功能分为 核心业务功能周边功能

周边功能在Spring 的面向编程AOP思想里,即被定义为切面。在面向切面编程AOP的思想里面,核心业务功能和切面功能分别 独立进行开发,然后把切面功能和核心业务功能“编织”在一起,这就叫AOP。

实例1(.xml方式配置AOP):

package com.ljf.service;
public class ProductService {
    public void doSomeService() {
        System.out.println("doSomeService...");
    }
}
package com.ljf.test;

import com.ljf.service.ProductService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml" );
        ProductService s = (ProductService) context.getBean("s");
        s.doSomeService();
    }
}

不使用切面的输出如下:


dosome.png
package com.ljf.aspect;

import org.aspectj.lang.ProceedingJoinPoint;

public class LoggerAspect {
    //log函数有一个形参 joinPoint 可以理解为断点
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();  //方法运行
        System.out.println("end log:" + joinPoint.getSignature().getName());
        return object;
    }
}

注:ProceedingJoinPoint 用于获取当前方法和参数。

    <!--声明业务对象-->
    <bean name="s" class="com.ljf.service.ProductService" />
    <!--声明辅助功能对象-->
    <bean id="loggerAspect" class="com.ljf.aspect.LoggerAspect" />

    <aop:config>

        <!--切入点,切入点的id叫loggerCutpoint,用来标记这个切入点-->
        <!--expression表示:满足expression中的调用方法之后,就回去进行切面操作,类似于触发了切面-->
        <aop:pointcut id="loggerCutpoint" expression="execution(* com.ljf.service.ProductService.*(..)) "/>

        <!--定义一个切面,id代表这个切面的名字,ref指方法所在的类,method代表的就是方法的名字-->
        <aop:aspect id="logAspect" ref="loggerAspect">
            <!--pointcut-ref="loggerCutpoint"表示这个切面与上面loggerCutpoint切点关联起来-->
            <aop:around pointcut-ref="loggerCutpoint" method="log"/>
            <!--log方法在LoggerAspect类中-->
        </aop:aspect>

    </aop:config>

加入日志切面后的 TestSpring 运行结果:

dosome_logger.png
AOP的过程主要分为两步:1.在业务类中插入切入点 2.将切入点和切面类关联起来
AOP术语
1.通知:

通知定义了切面要完成的工作内容和何时完成工作,就是什么时候去做辅助功能,功能具体是什么代码。
五种类型:

2.连接点:

在执行正常的功能时,能够插入切面的点。
连接点可以是调用方法时、抛出异常时、甚至修改字段时,在这些点,就可以去执行切面。

3.切面:

定义:切面是通知和切点的集合,通知和切点共同定义了切面的全部功能——它是什么,在何时何处完成其功能。
声明切面:

4.切点:

定义:如果通知定义了“什么”和“何时”。那么切点就定义了“何处”。切点会匹配通知所要织入的一个或者多个连接点。通常使用明确的类或者方法来指定这些切点。
作用:定义通知被应用的位置(在哪些连接点)
切入点的声明:切入点在Spring也是一个Bean。

<aop:config>  
   <aop:pointcut id="pointcut" expression="execution(* cn.javass..*.*(..))"/>  
   <aop:aspect ref="aspectSupportBean">  
      <aop:before pointcut-ref="pointcut" method="before"/>  
   </aop:aspect>  
</aop:config>  

实例2(注解方式AOP):

package com.ljf.service;

import org.springframework.stereotype.Component;

@Component("s")
public class ProductService {
    public void doSomeService() {
        System.out.println("doSomeService...");
    }
}
package com.ljf.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect  //表示这是一个切面
@Component  //表示这是一个bean,由Spring进行管理
public class LoggerAspect {
    //log函数有一个形参 joinPoint 可以理解为断点
    @Around(value = "execution(* com.ljf.service.ProductService.*(..))")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();  //方法运行
        System.out.println("end log:" + joinPoint.getSignature().getName());
        return object;
    }
}
    <!--扫描包,定义业务类和切面类-->
    <context:component-scan base-package="com.ljf.service" />
    <context:component-scan base-package="com.ljf.aspect" />
    <!--自动为spring容器中那些配置@aspectJ切面的bean创建代理,织入切面-->
    <aop:aspectj-autoproxy />
package com.ljf.test;

import com.ljf.service.ProductService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml" );
        ProductService s = (ProductService) context.getBean("s");
        s.doSomeService();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读