原文:https://github.com/x113773/testall/issues/12

1. 还是首先添加依赖(使用当前springboot的默认版本)
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

```
- 参考下面[官方文档](http://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/)的部分配置说明,可见aop是默认开启的,自动添加了@EnableAspectJAutoProxy注解
```
# AOP
spring.aop.auto=true # Add @EnableAspectJAutoProxy.
spring.aop.proxy-target-class=false # Whether subclass-based (CGLIB) proxies are to be created (true) as opposed to standard Java interface-based proxies (false).
```

2. 编写一个切面类,[AspectAdviceConfig.java](https://github.com/x113773/testall/blob/master/src/main/java/com/ansel/testall/aop/AspectAdviceConfig.java),里面定义了一个切点指示器和各种通知(Advice,也译作 增强)

```
package com.ansel.testall.aop;

import java.lang.reflect.Method;
import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.DeclareParents;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Aspect
@Component
public class AspectAdviceConfig {
private Logger logger = LoggerFactory.getLogger(this.getClass());

/**
* 定义切点指示器:com.ansel.testall.aop包下,带@RestController注解的类。
*/
@Pointcut("execution(* com.ansel.testall.aop..*(..)) and @annotation(org.springframework.web.bind.annotation.RestController)")
public void myPointcut() {
}

/**
* 前置通知,在目标方法完成之后调用通知,此时不会关 心方法的输出是什么
*/
@Before("myPointcut()")
public void beforeAdvice() {
System.out.println("Before--通知方法会在目标方法调用之前执行");
}

/**
* 后置通知,在目标方法完成之后调用通知,此时不会关 心方法的输出是什么
*/
@After("myPointcut()")
public void afterAdvice() {
System.out.println("After--通知方法会在目标方法返回或抛出异常后调用");
}

/**
* 返回通知,在目标方法成功执行之后调用,可以获得目标方法的返回值,但不能修改(修改也不影响方法的返回值)
*
* @param jp
* JoinPoint接口,可以获得连接点的一些信息
*
* @param retVal
* 目标方法返回值,和jp一样会由spring自动传入
*/
@AfterReturning(returning = "retVal", pointcut = "myPointcut()")
public void afterReturningAdvice(JoinPoint jp, Object retVal) {
retVal = retVal + " (@AfterReturning can read the return value, but it can't change the value!)";
System.out.println("AfterReturning--通知方法会在目标方法返回后调用; retVal = " + retVal);
System.out.println(jp.toLongString());
}

/**
* 异常通知,在目标方法抛出异常后调用通知
*/

@AfterThrowing("myPointcut()")
public void afterThrowingAdvice() {
System.out.println("AfterThrowing--通知方法会在目标方法抛出异常后调用");
}

/**
* 环绕通知,可以在目标方法调用前后,自定义执行内容。可以修改目标方法的返回值
*
* @param pjp
*/
@Around("myPointcut()")
public Object aroundAdvice(ProceedingJoinPoint pjp) {
Object retVal = null;
try {
System.out.println("Around--目标方法调用之前执行");

ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();

MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod(); // 获取被拦截的方法
String methodName = method.getName(); // 获取被拦截的方法名

logger.info("requset method name is: " + methodName);
logger.info("request URL is: " + request.getRequestURL().toString());
logger.info("request http method: " + request.getMethod());
logger.info("request arguments are: " + Arrays.toString(pjp.getArgs()));

retVal = pjp.proceed();
retVal = retVal + " (@Around can change the return value!)";

System.out.println("Around--目标方法返回后调用");
} catch (Throwable e) {
System.out.println("Around--目标方法抛出异常后调用");
}
return retVal;
}
}

```
3. 通过Advice可以某些方法增加一些功能,若要为某个对象增加新的方法,则要用到Introduction,编写另外一个切面AspectIntroductionConfig.java

```
package com.ansel.testall.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class AspectIntroductionConfig {

/**
* DeclareParents注解所标注的静态属性指明了要引入的接口;
* value属性指定了哪种类型的bean要引入该接口;
* defaultImpl属性指定了为引入功能提供实现的类。
*/
@DeclareParents(value = "com.ansel.testall.aop.AopService+", defaultImpl = IntroductionServiceImpl.class)
public static IntroductionService introductionService;
}

```
4. 编写两个接口AopService、IntroductionService,和2个实现AopServiceImpl、IntroductionServiceImpl

```
package com.ansel.testall.aop;

public interface AopService {
String myOwnMethod();
}
/***********************************/
package com.ansel.testall.aop;

public interface IntroductionService {
String IntroductionMethod();
}
/***********************************/
package com.ansel.testall.aop;

import org.springframework.stereotype.Service;

@Service
public class AopServiceImpl implements AopService {

@Override
public String myOwnMethod() {
return "this method is from AopService";
}

}
/***********************************/
package com.ansel.testall.aop;

import org.springframework.stereotype.Service;

@Service
public class IntroductionServiceImpl implements IntroductionService {

@Override
public String IntroductionMethod() {
return "this method from Introduction.";
}

}
```

5. 编写一个切点实例[AopController.java](https://github.com/x113773/testall/blob/master/src/main/java/com/ansel/testall/aop/AopController.java) (其实就是个普通的controller)

```
package com.ansel.testall.aop;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AopController {

/**
* 只注入AopService
*/
@Autowired
AopService aopService;

/**
* 测试AOP通知(Advice,也译作 增强)
*
* @return
*/
@RequestMapping(value = "/aop", method = RequestMethod.GET)
public String testAop() {
return "this is a AOP Advice test.";
}

/**
* 测试AOP引入(Introduction)
*
* @return
*/
@RequestMapping(value = "/aop/introdution", method = RequestMethod.GET)
public String testAopIntroduction() {
System.out.println(aopService.myOwnMethod());
//接口类型转换
IntroductionService introductionService = (IntroductionService) aopService;
System.out.println(introductionService.IntroductionMethod());
return "this is a AOP Introduction test.";
}
}

```

4. 启动项目,触发 localhost:8080/aop 请求,控制台输出结果如下:

```
Around--目标方法调用之前执行
2017-07-03 17:33:15.494 INFO 8548 --- [nio-8443-exec-7] tConfig$$EnhancerBySpringCGLIB$$5f4562fb : requset method name is: testAOP
2017-07-03 17:33:15.495 INFO 8548 --- [nio-8443-exec-7] tConfig$$EnhancerBySpringCGLIB$$5f4562fb : request URL is: https://localhost:8443/aop
2017-07-03 17:33:15.495 INFO 8548 --- [nio-8443-exec-7] tConfig$$EnhancerBySpringCGLIB$$5f4562fb : request http method: GET
2017-07-03 17:33:15.495 INFO 8548 --- [nio-8443-exec-7] tConfig$$EnhancerBySpringCGLIB$$5f4562fb : request arguments are: []
Before--通知方法会在目标方法调用之前执行
Around--目标方法返回后调用
After--通知方法会在目标方法返回或抛出异常后调用
AfterReturning--通知方法会在目标方法返回后调用; retVal = this is a AOP test. (@Around can change the return value!) (@AfterReturning can read the return value, but it can't change the value!)
execution(public java.lang.String com.ansel.testall.aop.AOPController.testAOP())
```

页面返回结果如下:
`this is a AOP test. (@Around can change the return value!)`

触发 localhost:8080/aop/introdution 请求,控制台输出结果如下:

![qq 20170704124306](https://user-images.githubusercontent.com/24689696/27815388-6151c8ca-60b6-11e7-86eb-aa5f2772caa5.png)

---
注:

`@Pointcut("execution(* com.ansel.testall.aop..*(..)) and @annotation(org.springframework.web.bind.annotation.RestController)")`

当我使用and操作符连接上面两个切点指示器,没有任何问题(把注解RestController换成RequestMapping也没问题)
然而,当我使用&&操作符连接上面两个切点指示器时,完全没有触发AspectJ,切面没有织入任何连接点(把注解RestController换成RequestMapping就没问题)

```
/**
* OK
*/
@Pointcut("execution(* com.ansel.testall.aop..*(..)) and @annotation(org.springframework.web.bind.annotation.RestController)")

/**
* not OK
*/
@Pointcut("execution(* com.ansel.testall.aop..*(..)) && @annotation(org.springframework.web.bind.annotation.RestController)")

/**
* OK
*/
@Pointcut("execution(* com.ansel.testall.aop..*(..)) and @annotation(org.springframework.web.bind.annotation.RequestMapping)")

/**
* OK
*/
@Pointcut("execution(* com.ansel.testall.aop..*(..)) && @annotation(org.springframework.web.bind.annotation.RequestMapping)")
```

Spring Boot1.5.4 AOP实例的更多相关文章

  1. Spring学习笔记IOC与AOP实例

    Spring框架核心由两部分组成: 第一部分是反向控制(IOC),也叫依赖注入(DI); 控制反转(依赖注入)的主要内容是指:只描述程序中对象的被创建方式但不显示的创建对象.在以XML语言描述的配置文 ...

  2. Java框架spring 学习笔记(十二):aop实例操作

    使用aop需要在网上下载两个jar包: aopalliance.jar aspectjweaver.jar 为idea添加jar包,快捷键ctrl+shift+alt+s,打开添加jar包的对话框,将 ...

  3. Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置

    用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...

  4. Spring.Net AOP实例

    Spring.Net和Log4net.NUnit.NHibernate一样,也是先从Java中流行开来,然后移植到了.NET当中,形成了.NET版的Spring框架.其官方网站为:http://www ...

  5. Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置(转)

    用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...

  6. Spring的IOC和AOP之深剖

    今天,既然讲到了Spring 的IOC和AOP,我们就必须要知道 Spring主要是两件事: 1.开发Bean:2.配置Bean.对于Spring框架来说,它要做的,就是根据配置文件来创建bean实例 ...

  7. Spring(6)—— AOP

    AOP(Aspect-OrientedProgramming)面向切面编程,与OOP完全不同,使用AOP编程系统被分为切面或关注点,而不是OOP中的对象. AOP的引入 在OOP面向对象的使用中,无可 ...

  8. 转-Spring Framework中的AOP之around通知

    Spring Framework中的AOP之around通知 http://blog.csdn.net/xiaoliang_xie/article/details/7049183 标签: spring ...

  9. spring之初识Ioc&Aop

    Spring框架的作用 spring是一个轻量级的企业级框架,提供了ioc容器.Aop实现.dao/orm支持.web集成等功能,目标是使现有的java EE技术更易用,并促进良好的编程习惯. Spr ...

随机推荐

  1. Jenkins获取git tags代码

    配置Jenkins获取git tag代码的方式其实方法很多,目前我使用比较多的主要是通过Git Parameter 来配置动态的获取最新tags代码,主要我们首先需要安装一下Git Parameter ...

  2. Java并发编程:Callable、Future和FutureTask的实现

    启动线程执行任务,如果需要在任务执行完毕之后得到任务执行结果,可以使用从Java 1.5开始提供的Callable和Future 下面就分析一下Callable.Future以及FutureTask的 ...

  3. C#超简单方法实现两个richtextbox控件滚动条同步滚动

    此文章属于作者原创,转载请注明,谢谢 有时候我们需要实现对照文章等,往往将文本放到两个richtextbox控件中,但是,如果我们需要同步滚动查看,来达到更好的观看效果. 当然,传统的方法重载控件或者 ...

  4. MIME协议在邮件中的应用详解

    1.定义 全称是多用途互联网邮件扩展(MIME,Multipurpose Internet Mail Extensions),在MIME出台之前,使用RFC 822只能发送基本的ASCII码文本信息, ...

  5. MYBATIS 简单整理与回顾

    这两天简单整理了一下MyBatis 相关api和jar包这里提供一个下载地址,免得找了 链接:http://pan.baidu.com/s/1jIl1KaE 密码:d2yl A.简单搭建跑项目 2.进 ...

  6. java虚拟机学习-慢慢琢磨JVM(2)

    1 JVM简介 JVM是我们Javaer的最基本功底了,刚开始学Java的时候,一般都是从“Hello World”开始的,然后会写个复杂点class,然后再找一些开源框架,比如Spring,Hibe ...

  7. Windows 7 下安装mysql-5.7.18-winx64.zip

    mysql-5.7以后压缩包安装有了不小的变化 第一步:到官网下载https://dev.mysql.com/downloads/mysql/ 第二步:解压到一个文件夹 第三步:配置环境变量 把;%M ...

  8. MPP 二、Greenplum数据加载

    Loading external data into greenplum database table using different ways... Greenplum 有常规的COPY加载方法,有 ...

  9. 使用Mingw编译wxSqlite3-3.0.5

    最近在学习wxWidgets,而且官方也出了3.0版本,貌似还不错的样子,准备做个小程序来练手.中间需要用到数据库看到很多人推荐wxSqlite3就去下来看看,以下是我使用TDM-GCC 4.8.1( ...

  10. Buffer深入分析

    Buffer Buffer的类图如下: 除了Boolean,其他基本数据类型都有对应的Buffer,但是只有ByteBuffer才能和Channel交互.只有ByteBuffer才能产生Direct的 ...