AspectJ:
AspectJ 是 Eclipse 基金组织的开源项目, 它是 Java 语言的一个 AOP 实现, 是最早, 功能比较强大的 AOP 实现之一, 对整套 AOP 机制都有较好的实现, 很多其他语言的 AOP 实现也借鉴或者采纳了 AspectJ 中的很多设计. 在 Java 领域, AspectJ 中的很多语法结构基本上已经成为 AOP 领域的标准.
-------------------------- 详情请看: https://blog.csdn.net/gavin_john/article/details/80156963
1) 首先到 AspectJ 官网下载 Jar 包: https://www.eclipse.org/aspectj/downloads.php
2) 下载完成后双击 jar 包, 开始安装 (花费不到 2 分钟)
3) 安装完成后, 把 文件夹里面的 jar 包导入到 IDEA 的 Spring 项目的 lib 文件夹中
添加到项目中:
经过这里已经配置好了!
开始编程:
1) 新建一个接口: AtithmeticCalculator
- package SpringAOP2;
- public interface AtithmeticCalculator {
- int add(int i, int j);
- int sub(int i, int j);
- int mul(int i, int j);
- int div(int i, int j);
- }
2) 建一个接口的实现类: AtithmeticCalculatorImpl
- package SpringAOP2;
- import org.springframework.stereotype.Component;
- @Component
- public class AtithmeticCalculatorImpl implements AtithmeticCalculator {
- @Override
- public int add(int i, int j) {
- int result=i+j;
- return result;
- }
- @Override
- public int sub(int i, int j) {
- int result=i-j;
- return result;
- }
- @Override
- public int mul(int i, int j) {
- int result=i*j;
- return result;
- }
- @Override
- public int div(int i, int j) {
- int result=i/j;
- return result;
- }
- }
3) 一个 main 方法 : 这个时候应该尝试运行, 验证 Bean 的自动装配是否成功, 切面的作用就是在不影响原有代码的基础上, 添加代码.
- public class Main {
- public static void main(String[] args) {
- ApplicationContext context=new ClassPathXmlApplicationContext("springConfig.xml");
- // 由于 AtithmeticCalculatorImpl 实现了 AtithmeticCalculator 接口, 所以强制转换必须用父类 AtithmeticCalculator 来定义
- AtithmeticCalculator atithmeticCalculator=(AtithmeticCalculator)context.getBean("atithmeticCalculatorImpl");
- int result=atithmeticCalculator.add(1,2);
- System.out.println("--------->"+result);
- result=atithmeticCalculator.div(8,2);
- System.out.println("--------->"+result);
- }
- }
4) 创建切面:
- @Component
- @Aspect
- class LoggingAspect {
- // 通过注解配置 bean 是正确的, 下面的代码有错误
- @Before("execution(public int SpringAOP2.AtithmeticCalculatorImpl.*(..))")
- private void beforeMethod(JoinPoint joinPoint) {
- String methodName=joinPoint.getSignature().getName();
- Object args[]=joinPoint.getArgs();
- System.out.println("方法名:"+methodName+", 参数:"+ Arrays.asList(args));
- System.out.println("这上面是前置切面啊");
- }
- @After("execution(public int SpringAOP2.AtithmeticCalculatorImpl.*(..))")
- private void afterMethod(){
- System.out.println("后置切面啊");
- }
- }
5) 通过配置文件进行配置: springConfig
- <!--1. 扫描, 自动配置 bean-->
- <context:component-scan base-package="SpringAOP2"/>
- <!--2. 使 AOP 生效 -->
- <aop:aspectj-autoproxy>
- </aop:aspectj-autoproxy>
文件夹:
这时就可以得到运行结果:
最后说一下在这过程中我遇到的问题:
AspectJ 遇到的坑:
报错信息:
Exception in thread "main" java.lang.ClassCastException: class com.sun.proxy.$Proxy9 cannot be cast to class SpringAOP2.AtithmeticCalculatorImpl
错误代码: 在 main 方法里边, 获取 Bean 实例的时候, 把获取到的对象转换成子类对象.
AtithmeticCalculator atithmeticCalculator=(AtithmeticCalculatorImpl)context.getBean("atithmeticCalculatorImpl");
解决方法 1: 报这个错, 只有一个原因, 就是转化的类型不对.
接口过父类的子类, 在强制转换的时候, 一定要用接口父类来定义.!!
- // 由于 AtithmeticCalculatorImpl 实现了 AtithmeticCalculator 接口, 所以强制转换必须用父类 AtithmeticCalculator 来定义
- AtithmeticCalculator atithmeticCalculator=(AtithmeticCalculator)context.getBean("atithmeticCalculatorImpl");
解决方法 2: 在 AOP 里面设置 proxy-target-class="true" 属性
- <aop:aspectj-autoproxy proxy-target-class="true">
- </aop:aspectj-autoproxy>
来源: http://www.bubuko.com/infodetail-3096889.html