Spring AOP面向切面编程,可以用来配置事务、做日志、权限验证、在用户请求时做一些处理等等。用@Aspect做一个切面,就可以直接实现。

·   本例演示一个基于@Aspect的小demo

  1、新建一个Maven工程

  2、引入相关maven依赖

 <project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>test-spring-aop</artifactId>
<version>0.0.1-SNAPSHOT</version> <!-- 定义maven变量 -->
<properties>
<!-- spring -->
<spring.version>5.1.4.RELEASE</spring.version>
</properties> <dependencies>
<!-- Spring IOC 核心容器 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency> <!-- Spring AOP 切面 模块 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.2</version>
</dependency> <dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency> </dependencies>
</project>

  3、新建一个SimpleAspect.java类,如下:

package com.test.aop;

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.Pointcut;
import org.springframework.stereotype.Component; /**
* @Auther: chenheng
* @Date: 2019/7/10 11:56
* @Description:
*/ @Aspect
@Component
public class SimpleAspect { /**
* 切点表达式:
* ..两个点表明多个,*代表一个
* 表达式代表切入com..service包下的所有类的所有方法,方法参数不限,返回类型不限。
* 其中访问修饰符可以不写,不能用*,,第一个*代表返回类型不限,第二个*表示所有类,第三个*表示所有方法,..两个点表示方法里的参数不限。
*/
private final String POINT_CUT = "execution(* com..service.*.*(..))"; /**
* 命名切点
* public 切点可访问性修饰符
* 与类可访问性修饰符的功能是相同的,它可以决定定义的切点可以在哪些类中可使用。
* pointCut 切点名称
* void 返回类型
*
* 因为命名切点仅利用方法名及访问修饰符的信息,
* 一般定义方法的返回类型为 void ,并且方法体为空
*/
@Pointcut(POINT_CUT)
public void pointCut(){} /**
* 在切点方法之前执行
* @param joinPoint
*/
@Before(value="pointCut()")
public void doBefore(JoinPoint joinPoint){
System.out.println("@Before:切点方法之前执行.....");
} /**
* 在切点方法之后执行
* @param joinPoint
*/
@After(value="pointCut()")
public void doAfter(JoinPoint joinPoint){
System.out.println("@After:切点方法之后执行.....");
} /**
* 切点方法返回后执行
* 如果第一个参数为JoinPoint,则第二个参数为返回值的信息
* 如果第一个参数不为JoinPoint,则第一个参数为returning中对应的参数
* returning:限定了只有目标方法返回值与通知方法参数类型匹配时才能执行后置返回通知,否则不执行,
* 参数为Object类型将匹配任何目标返回值
*/
@AfterReturning(value = "pointCut()",returning = "result")
public void doAfter(JoinPoint joinPoint,Object result){
System.out.println("@AfterReturning:切点方法返回后执行.....");
System.out.println("返回值:"+result);
} /**
* 切点方法抛异常执行
* 定义一个名字,该名字用于匹配通知实现方法的一个参数名,当目标方法抛出异常返回后,将把目标方法抛出的异常传给通知方法;
* throwing:限定了只有目标方法抛出的异常与通知方法相应参数异常类型时才能执行后置异常通知,否则不执行,
* 对于throwing对应的通知方法参数为Throwable类型将匹配任何异常。
* @param joinPoint
* @param exception
*/
@AfterThrowing(value = "pointCut()",throwing = "exception")
public void doAfterThrowing(JoinPoint joinPoint,Throwable exception){
System.out.println("@afterThrowing:切点方法抛异常执行.....");
} /**
*
* 属于环绕增强,能控制切点执行前,执行后,,用这个注解后,程序抛异常,会影响@AfterThrowing这个注解
*
* org.aspectj.lang.JoinPoint 接口表示目标类连接点对象,它定义这些主要方法。
* Object[] getArgs():获取连接点方法运行时的入参列表。
* Signature getSignature():获取连接点的方法签名对象。
* Object getTarget():获取连接点所在的目标对象。
* Object getThis():获取代理对象。
* @param pjp
* @return
* @throws Throwable
*/
@Around(value="pointCut()")
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("@Around:切点方法环绕start.....");
Object[] args = pjp.getArgs();
Object o = pjp.proceed(args);
System.out.println("@Around:切点方法环绕end.....");
return o;
} }

  4、新建一个AspectService.java类,如下:

 package com.test.service;

 import org.springframework.stereotype.Service;

 @Service
public class AspectService { public String sayHi(String name)
{
System.out.println("方法:sayHi 执行中 ....");
return"Hello, " + name;
} public void excuteException()
{
System.out.println("方法:excuteException 执行中 ....");
int n = 1;
if(n > 0) {
throw new RuntimeException("数据异常");
}
} }

  5、新建一个spring配置文件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"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.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:component-scan base-package="com.test"></context:component-scan> <!-- 配置开启@Aspect支持 -->
<aop:aspectj-autoproxy proxy-target-class="true" /> </beans>

  6、新建一个测试类,如下:

 package com.test.main;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.test.service.AspectService; public class TestMain { public static void main(String[] args) { // ClassPathXmlApplicationContext默认是加载src目录下的xml文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); AspectService aservice = context.getBean(AspectService.class);
System.out.println("\n===========普通调用=============\n"); aservice.sayHi("hd"); System.out.println("\n===========异常调用=============\n"); aservice.excuteException(); System.out.println("\n========================\n");
} }

  7、运行测试类,结果如下:

    

【Spring】基于@Aspect的AOP配置的更多相关文章

  1. 【AOP】基于@Aspect的AOP配置

    基于spring cloud的aop配置 1,启动类MemberAppliaction增加注解 @Import({SwaggerConfiguraion.class, WebMvcAutoConfig ...

  2. 基于@Aspect的AOP配置

    1. Spring 除了支持Schema 方式配置 AOP,还支持注解方式:使用 @Aspect 来配置 2. Spring 默认不支持 @Aspect 风格的切面声明,通过如下配置开启@Aspect ...

  3. 阶段3 2.Spring_08.面向切面编程 AOP_9 spring基于注解的AOP配置

    复制依赖和改jar包方式 src下的都复制过来. 复制到新项目里了 bean.xml里面复制上面一行代码到下面.把aop改成context. 配置spring容器创建时要扫描的包 Service的配置 ...

  4. spring 注解 之 AOP基于@Aspect的AOP配置

    Spring AOP面向切面编程,可以用来配置事务.做日志.权限验证.在用户请求时做一些处理等等.用@Aspect做一个切面,就可以直接实现. 1.首先定义一个切面类,加上@Component  @A ...

  5. 基于XML的AOP配置

    创建spring的配置文件并导入约束 此处要导入aop的约束 <?xml version="1.0" encoding="UTF-8"?> < ...

  6. Spring 基于 AspectJ 的 AOP 开发

    Spring 基于 AspectJ 的 AOP 开发 在 Spring 的 aop 代理方式中, AspectJ 才是主流. 1. AspectJ 简介 AspectJ 是一个基于 java 语言的 ...

  7. spring的基于xml的AOP配置案例和切入点表达式的一些写法

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  8. [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.

    前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...

  9. 基于Schema的AOP 配置使用详解

    原文地址:http://jinnianshilongnian.iteye.com/blog/1418598 基于Schema的AOP从Spring2.0之后通过"aop"命名空间来 ...

随机推荐

  1. 【Java】《Java程序设计基础教程》第四章学习

    4.1 类的封装 封装指的是将东西包装在一起,然后以新的完整形式呈现.包含两个意义: 1). 把对象的全部属性和方法结合在一起,形成一个不可分割的独立单位(即对象). 2). 信息隐藏,即尽可能隐藏对 ...

  2. 接口测试工具soapUI

    一.下载 官网:https://www.soapui.org/ 链接:https://pan.baidu.com/s/15LITTKIvfIoAq5IBpID8gA 提取码:l7p6 二.SoapUI ...

  3. CF1106F Lunar New Year and a Recursive Sequence——矩阵快速幂&&bsgs

    题意 设 $$f_i = \left\{\begin{matrix}1 , \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \  i < k\\ ...

  4. MySQL Innodb引擎调优

    介绍: Innodb给MYSQL提供了具有提交,回滚和崩溃恢复能力的事务安全(ACID兼容)存储引擎.Innodb锁定在行级并且也在SELECT语句提供一个Oracle风格一致的非锁定读.这些特色增加 ...

  5. PHP流程控制之分支结构switch语句的使用

    分支结构switch语句的使用 还记得我们最开始讲了这么一个故事: 王同学家里头特别有钱,所以他的行程方式和正常人的又有些不一样. 他的出行方式呢有6种,如下: 1,司机开车2,民航3,自己家的专机4 ...

  6. noi.ac #44 链表+树状数组+思维

    \(des\) 给出长度为 \(n\) 的序列,全局变量 \(t\),\(m\) 次询问,询问区间 \([l, r]\) 内出现次数为 \(t\) 的数的个数 \(sol\) 弱化问题:求区间 \([ ...

  7. 【数论】P1029 最大公约数和最小公倍数问题

    题目链接 P1029 最大公约数和最小公倍数问题 思路 如果有两个数a和b,他们的gcd(a,b)和lcm(a,b)的乘积就等于ab. 也就是: ab=gcd(a,b)*lcm(a,b) 那么,接下来 ...

  8. 10分钟手把手教你运用Python实现简单的人脸识别

    欲直接下载代码文件,关注我们的公众号哦!查看历史消息即可! 前言:让我的电脑认识我 我的电脑只有认识我,才配称之为我的电脑! 今天,我们用Python实现高大上的人脸识别技术! Python里,简单的 ...

  9. Codeforces 1137F Matches Are Not a Child's Play [LCT]

    Codeforces 很好,通过这题对LCT的理解又深了一层. 思路 (有人说这是套路题,然而我没有见过/kk) 首先发现,删点可以从根那里往下删,非常难受,所以把权值最大的点提为根. 然后考虑\(x ...

  10. Jmeter5.1 Plugins Manager配置dummy使用jp@gc - Dummy Sampler

    背景和目的 最近想使用dummy进行mockserver服务器的模拟来实现正则表达式测试,但是发现在选项中没有Plugins Manager可供选择 如果本文对你有帮助,请关注我哦,一起进步.接下来看 ...