使用Spring进行面向方面编程

记录方法执行

新建Maven项目,添加依赖

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.1</version>
    </dependency>
</dependencies>

新建org.beginning.aop.ExecutionTimeLoggingSpringAop类,该类实现MethodBeforeAdvice,AfterReturningAdvice接口

public class ExecutionTimeloggingSpringAop implements MethodBeforeAdvice, AfterReturningAdvice {

    private long startTime = 0;

    public void before(Method method, Object[] args, Object target)throws Throwable{

        startTime = System.nanoTime();

    }

    public void afterReturning(Object returnValue, Method method, Object[] orgs, Object target){

        long elapseTime = System.nanoTime() - startTime;

        String className = target.getClass().getCanonicalName();

        String methodName = method.getName();

        System.out.println("Excetion of " + className + "#" + methodName + " end in " + new BigDecimal(elapseTime).divide(new BigDecimal(1000000)) + " milliseconds");

    }

}

在类路径(src/main/resources文件夹)下创建一个应用程序上下文配置文件。其中提供了通知和切入点定义

<?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:context="http://www.springframework.org/schema/context"

       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/context

       http://www.springframework.org/schema/context/spring-context-4.0.xsd

       http://www.springframework.org/schema/aop

       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">



    <context:annotation-config />

    <context:component-scan base-package="org.beginning.component" />



    <bean id="executionTimeLoggingSpringAop" class="org.beginning.aop.ExecutionTimeloggingSpringAop" />



    <aop:config>

        <aop:pointcut id="executionTimeLoggingPointcut" expression="execution(public * * (..))" />



        <aop:advisor id="executionTimeLoggingAdvisor"

                     advice-ref="executionTimeLoggingSpringAop"

                     pointcut-ref="executionTimeLoggingPointcut" />

    </aop:config>







</beans>

在包org.beginning.component下新建Bean

@Component

public class MyBean {

    public void sayHello(){

        System.out.println("Hello ...!");

    }

}
@Component

public class MyOtherBean {

    public void sayHelloDelayed() throws InterruptedException{

        Thread.sleep(1000);

        System.out.println("Hello ...!");

    }

}

在Main类中调用Bean的方法

public class Main {

    public static void main(String[] args) throws Throwable{

        ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml",Main.class);

        MyBean myBean = context.getBean(MyBean.class);

        myBean.sayHello();



        MyOtherBean myOtherBean = context.getBean(MyOtherBean.class);

        myOtherBean.sayHelloDelayed();

    }

}

示例说明

在实际方法调用前,调用了MethodBeforeAdvice通知,而在实际方法返回且没有抛出任何异常的时候调用AfterReturingAdvice通知。

在applicationContext.xml配置中,配置aop的切入点,与模式public**()相匹配的任何公共方法都会调用通知。

在通知中会接受Method,Object等参数。

通知的类型

Before    在接合点之前执行通知

After Returning  在接合点执行完成之后执行通知

After Throwing 如果从接合点抛出了任何异常,则执行通知

After(finally)  接合点执行完毕之后,不管是否抛出一个异常,都执行通知

Around  在接合点周围执行通知,意味着可能在接合点之前以及以后执行通知

使用AspectJ记录方法执行的时间

在org.beginning.component包下新建ExecutionTimeLoggingAspectJ类

@Component

@Aspect

public class ExecutionTimeLoggingAspectJ {

    @Around("execution(public * * (..))")

    public Object profile(ProceedingJoinPoint pjp) throws Throwable{

        long startTime = System.nanoTime();



        String className = pjp.getTarget().getClass().getCanonicalName();

        String methodName = pjp.getSignature().getName();



        Object output = pjp.proceed();



        long elapsedTime = System.nanoTime() - startTime;

        System.out.println("Excetion of " + className + "#" + methodName + " end in " + new BigDecimal(elapsedTime).divide(new BigDecimal(1000000)) + " milliseconds");



        return output;

    }

}

修改applicationContext.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:context="http://www.springframework.org/schema/context"

       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/context

       http://www.springframework.org/schema/context/spring-context-4.0.xsd

       http://www.springframework.org/schema/aop

       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">



    <context:annotation-config />

    <context:component-scan base-package="org.beginning.component" />



    <aop:aspectj-autoproxy />







</beans>

在main中调用bean方法

public class Main {

    public static void main(String[] args) throws Throwable{

        ApplicationContext context = new ClassPathXmlApplicationContext("/applicationContext.xml",Main.class);

        MyBean myBean = context.getBean(MyBean.class);

        myBean.sayHello();



        MyOtherBean myOtherBean = context.getBean(MyOtherBean.class);

        myOtherBean.sayHelloDelayed();

    }

}

示例说明

本例是在上一个例子的基础上进行的,把通知由接口和xml配置改为用AspectJ的注解来实现。

不同的点有在通知上用了@Around注解,并且通知的参数用了proceedingJoinPoint

表示通知在方法之前的周围执行。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值