概念:

切面(aspect):用来切插业务方法的类。
连接点(joinpoint):是切面类和业务类的连接点,其实就是封装了业务方法的一些基本属性,作为通知的参数来解析。
通知(advice):在切面类中,声明对业务方法做额外处理的方法。
切入点(pointcut):业务类中指定的方法,作为切面切入的点。其实就是指定某个方法作为切面切的地方。
目标对象(target object):被代理对象。
AOP代理(aop proxy):代理对象。

AOP通知类型:
前置通知(before advice):在切入点之前执行。
后置通知(after returning advice):在切入点执行完成后,执行通知。
环绕通知(around advice):包围切入点,调用方法前后完成自定义行为。
异常通知(after throwing advice):在切入点抛出异常后,执行通知

依赖包:

    <dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.2.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>

Code:

package com.qhong;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component; public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Person person=(Person)context.getBean("babyPerson");
person.eatBreakfast();
System.out.println("===================================================");
person.eatLunch();
System.out.println("===================================================");
person.eatSupper();
System.out.println("===================================================");
person.drink("可乐");
System.out.println("===================================================");
}
} interface Person {
public void eatBreakfast();
public void eatLunch();
public void eatSupper();
public String drink(String name);
} @Component
class BabyPerson implements Person{ @Override
public void eatBreakfast() {
System.out.println("小Baby正在吃早餐");
} @Override
public void eatLunch() {
System.out.println("小Baby正在吃午餐");
} @Override
public void eatSupper() {
System.out.println("小Baby正在吃晚餐");
} @Override
public String drink(String name) {
return "小Baby在喝:"+name;
}
} @Component
@Aspect
class AdivceMethod { @Before("execution(* com.qhong.BabyPerson.*(..))")
// 匹配BabyPerson类所有的方法,注意*和com之间有个空格
public void beforeEat() {
System.out
.println("-------------------这里是前置增强,吃饭之前先洗小手!--------------------");
} @After("execution(* eatLunch(..))")
// 匹配该工程下所有的eatLunch方法
public void afterEat() {
System.out
.println("-------------------这里是后置增强,午饭吃完要睡午觉!--------------------");
} @Around("execution(* com.qhong.BabyPerson.eatSupper())")
// 匹配该工程下BabyPerson的eatLunch方法
public Object aroundEat(ProceedingJoinPoint pjp) throws Throwable {
System.out
.println("-------------------这里是环绕增强,吃晚饭前先玩一玩!-------------------");
Object retVal = pjp.proceed();
System.out
.println("-------------------这里是环绕增强,晚饭吃完后要得睡觉了!-------------------");
return retVal;
}
@AfterReturning(returning="rvt",pointcut="execution(* com.qhong.BabyPerson.drink(..))")
public void log(Object rvt) {
System.out
.println("-------------------这里是AfterReturning增强-------------------");
System.out.println("获取小Baby正在喝的饮料"+rvt);
System.out.println("记录每天喝的饮料容量"); }
}

beans.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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <!-- 指定自动搜索bean组件、自动搜索切面类 -->
<context:component-scan base-package="com.qhong"/>
<!-- 启动@AspectJ支持 -->
<!-- proxy-target-class默认"false",更改为"ture"使用CGLib动态代理 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
Result:
-------------------这里是前置增强,吃饭之前先洗小手!--------------------
小Baby正在吃早餐
===================================================
-------------------这里是前置增强,吃饭之前先洗小手!--------------------
小Baby正在吃午餐
-------------------这里是后置增强,午饭吃完要睡午觉!--------------------
===================================================
-------------------这里是环绕增强,吃晚饭前先玩一玩!-------------------
-------------------这里是前置增强,吃饭之前先洗小手!--------------------
小Baby正在吃晚餐
-------------------这里是环绕增强,晚饭吃完后要得睡觉了!-------------------
===================================================
-------------------这里是前置增强,吃饭之前先洗小手!--------------------
-------------------这里是AfterReturning增强-------------------
获取小Baby正在喝的饮料小Baby在喝:可乐
记录每天喝的饮料容量
===================================================

Spring + AspectJ(基于注解:通过 AspectJ execution 表达式拦截方法)
package com.qhong;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component; public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
com.qhong.Greeting greeting = (com.qhong.Greeting) context.getBean("greetingImpl");
greeting.sayHello("Jack");
}
} interface Greeting {
void sayHello(String name);
} @Component
class GreetingImpl implements Greeting {
@Override
public void sayHello(String name) {
System.out.println("Hello! " + name);
}
} @Aspect
@Component
class GreetingAspect {
@Around("execution(* com.qhong.GreetingImpl.*(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
before();
Object result = pjp.proceed();
after();
return result;
} private void before() {
System.out.println("Before");
} private void after() {
System.out.println("After");
}
}

spring.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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.qhong"/> <aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>

Result:

Before
Hello! Jack
After

遇到的bug

java.lang.NoClassDefFoundError: org/springframework/beans/factory/NoUniqueBeanDefinitionException

遇到这个问题应该是groupid是org.springframework的各个依赖之间版本相差太大造成的问题,全都改成统一的就ok了。

http://tonylit.me/2016/06/29/spring%20aop-AspectJ%E6%B3%A8%E8%A7%A3%E6%96%B9%E5%BC%8F/

http://www.yiibai.com/spring/spring-aop-aspectj-annotation-example.html

http://blog.csdn.net/xiaoxian8023/article/details/17285809

http://www.kancloud.cn/evankaka/springlearning/119670

http://git.oschina.net/lujianing/aop_demo

https://my.oschina.net/huangyong/blog/159788

https://my.oschina.net/huangyong/blog/161338

https://my.oschina.net/huangyong/blog/161402

https://my.oschina.net/huangyong/blog/160769

https://my.oschina.net/huangyong/blog/170494

AOP AspectJ注解的更多相关文章

  1. Spring学习(十八)----- Spring AOP+AspectJ注解实例

    我们将向你展示如何将AspectJ注解集成到Spring AOP框架.在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法. 常见AspectJ的注解: @Before – 方法 ...

  2. Spring——AOP(面向切面编程)@AspectJ注解方式

    一.什么是AOP? AOP: (Aspect Oriented Programming)即面向切面编程. 试想这样的场景:项目中需要在业务方法执行完打印日志记录.最笨的办法就是在每个方法核心业务执行完 ...

  3. SSH深度历险(十) AOP原理及相关概念学习+AspectJ注解方式配置spring AOP

    AOP(Aspect Oriented Programming),是面向切面编程的技术.AOP基于IoC基础,是对OOP的有益补充. AOP之所以能得到广泛应用,主要是因为它将应用系统拆分分了2个部分 ...

  4. Spring学习之旅(八)Spring 基于AspectJ注解配置的AOP编程工作原理初探

    由小编的上篇博文可以一窥基于AspectJ注解配置的AOP编程实现. 本文一下未贴出的相关代码示例请关注小编的上篇博文<Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AO ...

  5. Spring学习之旅(七)基于XML配置与基于AspectJ注解配置的AOP编程比较

    本篇博文用一个稍复杂点的案例来对比一下基于XML配置与基于AspectJ注解配置的AOP编程的不同. 相关引入包等Spring  AOP编程准备,请参考小编的其他博文,这里不再赘述. 案例要求: 写一 ...

  6. spring AOP 之二:@AspectJ注解的3种配置

    @AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...

  7. spring-AOP框架(基于AspectJ注解配置AOP)

    基于AspectJ注解配置AOP 1.加入jar包: 要在Spring应用中使用AspectJ注解,必须在classpath下包含AspectJ类库:aopalliance.jar.aspectj.w ...

  8. Spring使用AspectJ注解和XML配置实现AOP

    本文演示的是Spring中使用AspectJ注解和XML配置两种方式实现AOP 下面是使用AspectJ注解实现AOP的Java Project首先是位于classpath下的applicationC ...

  9. spring AOP编程--AspectJ注解方式

    1. AOP 简介 AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, ...

随机推荐

  1. android基础组件---->Spinner的使用

    Spinner提供了一个快速的方式从集合中选择值.在默认状态下,一个Spinner显示的是当前选择的值.触摸Spinner会显示一个下拉菜单,用户可以从中选择一个值.今天我们就开始Spinner的学习 ...

  2. c++ 利用new动态的定义二维数组

    #include <iostream> using namespace std; int main() { , col = ; // key code: 申请空间 int **edge = ...

  3. 【Android】 ImageView.ScaleType设置图解

    ImageView的Scaletype决定了图片在View上显示时的样子,如进行何种比例的缩放,及显示图片的整体还是部分,等等. 设置的方式包括: 1. 在layout xml中定义android:s ...

  4. zeptojs使用及介绍,手机端为什么一定要用zeptojs?

    zepto是和jQuery和类似的框架,由于jQuery内置了很多解决浏览器兼容性的代码,导致jQuery过于臃肿,对于移动端来说,不需要太过于考虑浏览器的兼容,所以就诞生zeptojs. 总的来说: ...

  5. 三维凸包求凸包表面的个数(HDU3662)

    3D Convex Hull Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  6. vscode 代码保存时自动格式化成 ESLint 风格

    vscode 的默认的代码格式化 alt+shift+f (windows) 无法通过 eslint 的代码风格检查,,, 比如: 4个空格和2个空格(ESLint) 字符串用单引号(ESLint) ...

  7. 使用 Capistrano 进行自动化部署

    最近在折腾这个,弄了好多次都不成功,看了官方文档和很多博客,都没有说清楚,因此,我觉得有必要把它记录下来,以帮助更多像我这样被弄得烦躁的人. 首先是安装,其实 Ubuntu 上面安装 Capistra ...

  8. HDU 5652 India and China Origins(并查集)

    India and China Origins Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  9. 项目无法运行iPhone5模拟器

    公司没有iPhone5真机, 有人反馈iPhone5有bug, 只能用模拟器验证bug, 但是使用iPhone5项目编译不过,报错: 注释掉相关引用代码,去掉 库

  10. 如何修改git分支名名称

    1. 修改本地git分支名称指令 git branch -m oldBranchName newBranchName 2. 修改远程仓库(github)上的分支名称 git本地分支名已修改,只需推送到 ...