为了账号安全,请及时绑定邮箱和手机立即绑定

java spring aop 简易版

标签:
Java

java spring aop
1、新建java 项目
2、新建lib 文件夹
3、jar 包
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.1.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
4、类路径下新建spring配置文件
5、新建一个接口,有四个方法:加减乘除;
6、实现类,实现5 中生命的方法
7、新建一个类记录日志;作用:在方法调用前打印日志,方法结束后打印日志,方法返回值进行接收,如果方法执行抛出异常要对异常信息进行输出
8、先不通过配置文件的方式,aspectj 表达式通过配置文件的方式一下子不太容易理解

代码:
package com.curtis.spring.aop.calculate;
public interface ICalculate {
    int add(int x, int y);
    int sub(int x, int y);
    int mul(int x, int y);
    int div(int x, int y);
}

package com.curtis.spring.aop.calculate;
import org.springframework.stereotype.Component;
@Component(value = "calculate")
public class CalculateImpl implements ICalculate {
    @Override
    public int add(int x, int y) {
        int result = x + y;
        return result;
    }
    @Override
    public int sub(int x, int y) {
        int result = x - y;
        return result;
    }
    @Override
    public int mul(int x, int y) {
        int result = x * y;
        return result;
    }
    @Override
    public int div(int x, int y) {
        int result = x / y;
        return result;
    }
}

package com.curtis.spring.aop.calculate;
import java.util.Arrays;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class CalculateLogging {    
    // 前置通知
    @Before(value = "execution(public int com.curtis.spring.aop.calculate.ICalculate.*(int,int))")
    public void beforeMethod(JoinPoint joinPoint) {
        System.out.println(
                joinPoint.getSignature().getName() + " method start with " + Arrays.asList(joinPoint.getArgs()));
    }
    // 后置通知
    @After(value = "execution(public int com.curtis.spring.aop.calculate.ICalculate.*(int,int))")
    public void afterMethod(JoinPoint joinPoint) {
        System.out.println(joinPoint.getSignature().getName() + " method end");
    }
    // 返回通知,如果执行主体方法有返回值,对返回值进行接收
    @AfterReturning(value = "execution(* com.curtis.spring.aop.calculate.ICalculate.*(..))", returning = "result")
    public void afterReturn(JoinPoint joinPoint, Object result) {
        System.out.println(joinPoint.getSignature().getName() + " method result is " + result);
    }
    // 异常通知,当方法执行时如果有异常,则将异常信息进行捕获
    @AfterThrowing(value = "execution(* com.curtis.spring.aop.calculate.ICalculate.*(..))", throwing = "e")
    public void afterThrowing(JoinPoint joinPoint, Exception e) {
        System.out.println(joinPoint.getSignature().getName() + " method exception is " + e);
    }
    // 任意修饰符+任意返回值
    // 接口类中的所有方法
    // 两点:任意参数
}

package com.curtis.spring.aop.calculate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestCalulate {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        ICalculate calculate = null;
        calculate = ctx.getBean("calculate", ICalculate.class);
        int result = calculate.add(2, 3);
        result = calculate.div(10, 5);
    }
}

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
    <context:component-scan base-package="com.curtis.spring.aop.calculate" />
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
点击查看更多内容
4人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消