AOP

切面就像一把菜刀,将Java处理业务流程进行分割,在分割处添加特定的业务处理。主要应用于声明事务、安全和缓存。在本文中,主要介绍两种切面的实现方法--Java配置和XML配置。

Java配置

  • 创建Java类

    创建一个Music的Java类,用于声明切点
@Component
public class Music { public void perform(int num){
System.out.println("music");
}
}
  • 创建切面

    创建Aop Java类,并声明为切面。声明为切面使用注解@Aspect,同时,切面必须是一个Bean。同时,声明一个切点,避免创建通知的时候重复使用过长的表达式。
@Component
@Aspect
public class Aop { @Pointcut("execution(** com.tidas.spring.fourth.aopjavaconfig.Music.*(..))")
public void performer(){} @Before("performer()")
public void beforee(){
System.out.println("before");
} @After("performer()")
public void afterr(){
System.out.println("after");
} @AfterReturning("performer()")
public void afterReturning(){
System.out.println("afterreturning");
} @AfterThrowing("performer()")
public void throwingg(){
System.out.println("throwing");
}
  • 创建Java配置类

    创建JavaConfiguration 类,创建Bean工厂,在这里需要使用@EnableAspectAutoProxy注解启动Spring切面功能
@Configuration
@EnableAspectJAutoProxy
@ComponentScan
public class JavaConfiguration {
}
  • 创建测试类

    创建Main测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=JavaConfiguration.class)
public class Main {
@Autowired
private Music music;
@Test
public void test(){
music.perform(3);
}
}
  • 为通知传递参数

    希望将声明为切点方法中的参数传递到通知当中其,则需要在声明切点的使用指明args参数,在Java中使用&& 在xml中使用and,下面是创建为通知传递参数的切面:
/*@Pointcut("execution(** com.tidas.spring.fourth.aopjavaconfig.Music.perform(int)) && args(number)")
public void performer(int number){}
@Before("performer(number)")
public void beforee(int number){
System.out.println("before" + number);
}
@After("performer(number)")
public void afterr(int number){
System.out.println("after" + number+2);
}
@AfterReturning("performer(number)")
public void afterReturning(int number){
System.out.println("afterreturning" + number+1);
}
@AfterThrowing("performer(number)")
public void throwingg(int number){
System.out.println("throwing" + number +3);
}
  • 另外,还可以使用@Around穿件环绕通知,被声明为环绕通知的方法需要包含参数ProceedingJoinPoint,通过ProceedingJoinPoint.proceed()来区分前置 通知和后置通知,通过try...catch来获取异常通知
@Component
@Aspect
public class AopSecond {
@Pointcut("execution(** com.tidas.spring.fourth.aopjavaconfig.Music.perform(..))")
public void performer(){} @Around("performer()")
public void per(ProceedingJoinPoint jb){
try{
System.out.println("beforeer");
jb.proceed();
System.out.println("afterr");
}catch(Throwable eThrowable){
System.out.println("exception");
}
}
}

XML中配置切面

  • 创建Java类
@Component
public class AopXml {
public void performA(){
System.out.println("performA");
}
public void performB(){
System.out.println("performB");
}
}
  • 创建切面类
@Component
public class AopConfug {
public void beforee(){
System.out.println("before");
}
public void afterr(){
System.out.println("after");
}
public void aroundd(ProceedingJoinPoint pb) throws Throwable{
System.out.println("beforeeee");
pb.proceed();
System.out.println("afterrrr");
}
}
  • 创建xml配置文件,其中需要添加xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ,来支持component-scan,同时,还需要配置<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>支持自动注入
<?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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
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">
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<context:component-scan base-package="com.tidas.spring.fourth.aopxml"/>
<!-- 无参数 -->
<aop:config>
<aop:aspect ref="aopConfug">
<aop:pointcut expression="execution(** com.tidas.spring.fourth.aopxml.AopXml.*(..))" id="aopxmll"/>
<!-- 执行AopXml中的任何方法,都会通知切面 -->
<aop:before pointcut-ref = "aopxmll" method="beforee"/> <aop:after pointcut-ref = "aopxmll" method="afterr"/> <aop:around pointcut-ref = "aopxmll" method="aroundd"/>
<!-- 对于环绕通知,其实在xml中声明和其它通知声明一样,没有参数,而参数还是在具体的方法中就好 -->
</aop:aspect>
</aop:config>
</beans>
  • 为通知传递参数

    创建带参数的通知
//有参数
/*public void beforee(int number){
System.out.println("number:" + number);
}*/

创建带参数的方法

//有参数
public void perform(int number){
System.out.println("perform2");
}

切面和切点的配置,在xml中使用and连接args参数

<aop:config>
<aop:aspect ref="aopConfug">
<aop:pointcut expression="execution(** com.tidas.spring.fourth.aopxml.AopXml.*(int)) and args(number)" id="aopxmll"/> <aop:before pointcut-ref = "aopxmll" method="beforee"/>
</aop:aspect>
</aop:config>

通过切面为Java引入新的功能

Spring--AOP(面向切面)编程的更多相关文章

  1. 详细解读 Spring AOP 面向切面编程(二)

    本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...

  2. 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~

    简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...

  3. spring AOP面向切面编程学习笔记

    一.面向切面编程简介: 在调用某些类的方法时,要在方法执行前或后进行预处理或后处理:预处理或后处理的操作被封装在另一个类中.如图中,UserService类在执行addUser()或updateUse ...

  4. 【Spring系列】Spring AOP面向切面编程

    前言 接上一篇文章,在上午中使用了切面做防重复控制,本文着重介绍切面AOP. 在开发中,有一些功能行为是通用的,比如.日志管理.安全和事务,它们有一个共同点就是分布于应用中的多处,这种功能被称为横切关 ...

  5. 从源码入手,一文带你读懂Spring AOP面向切面编程

    之前<零基础带你看Spring源码--IOC控制反转>详细讲了Spring容器的初始化和加载的原理,后面<你真的完全了解Java动态代理吗?看这篇就够了>介绍了下JDK的动态代 ...

  6. Spring AOP面向切面编程详解

    前言 AOP即面向切面编程,是一种编程思想,OOP的延续.在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等.在阅读本文前希望您已经对Spring有一定的了解 注:在能对代码进行添 ...

  7. Spring AOP 面向切面编程相关注解

    Aspect Oriented Programming 面向切面编程   在Spring中使用这些面向切面相关的注解可以结合使用aspectJ,aspectJ是专门搞动态代理技术的,所以比较专业.   ...

  8. Spring AOP 面向切面编程入门

    什么是AOP AOP(Aspect Oriented Programming),即面向切面编程.众所周知,OOP(面向对象编程)通过的是继承.封装和多态等概念来建立一种对象层次结构,用于模拟公共行为的 ...

  9. 详细解读 Spring AOP 面向切面编程(一)

    又是一个周末, 今天我要和大家分享的是 AOP(Aspect-Oriented Programming)这个东西,名字与 OOP 仅差一个字母,其实它是对 OOP 编程方式的一种补充,并非是取而代之. ...

  10. Spring Aop面向切面编程&&自动注入

    1.面向切面编程 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面的过程叫做面向切面编程 2.常用概念 原有功能:切点,pointcut 前置通知:在切点之前执行的功能,befor ...

随机推荐

  1. 「mysql优化专题」详解引擎(InnoDB,MyISAM)的内存优化攻略?(9)

    注意:以下都是在MySQL目录下的my.ini文件中改写(技术文). 一.InnoDB内存优化 InnoDB用一块内存区域做I/O缓存池,该缓存池不仅用来缓存InnoDB的索引块,而且也用来缓存Inn ...

  2. Execution default of goal org.springframework.boot:spring-boot-maven-plugin:1.5.6.RELEASE:repackage failed: Unable to find main class

    异常 [INFO] --- spring-boot-maven-plugin:1.5.6.RELEASE:repackage (default) @ spring-boot-starter-log - ...

  3. 0基础手把手教你搭建webpack运行打包项目(未完待续)

    这些天在项目之余的时间学习了webpack打包项目的东西,非常荣幸的找到一些大神的文章来学习,死劲嚼了几天,终于略知一二.在以后的工作上还需继续学习,下面我将分享我这几天学到的一点东西,希望能让我一个 ...

  4. SpringMVC框架(二)注解 (转)

    原文地址:http://www.cnblogs.com/yjq520/p/6734422.html 1.@Controller @Controller 用于标记在一个类上,使用它标记的类就是一个Spr ...

  5. linux使用freetds 连接连远程服务器sqlservser2012

    1.下载:freetds-patched.tar.gz  http://www.freetds.org/software.html http://www.freetds.org/userguide/c ...

  6. iOS 数据加密方案

    iOS安全攻防(二十三):Objective-C代码混淆 提交用户的隐私数据 一定要使用POST请求提交用户的隐私数据GET请求的所有参数都直接暴露在URL中请求的URL一般会记录在服务器的访问日志中 ...

  7. [机器学习系列] k-近邻算法(K–nearest neighbors)

    C++ with Machine Learning -K–nearest neighbors 我本想写C++与人工智能,但是转念一想,人工智能范围太大了,我根本介绍不完也没能力介绍完,所以还是取了他的 ...

  8. 关于Qt Designer程序/UI文件打开未响应的解决方法

    最近完成一个项目,到最后关头用QtCreator无法打开UI文件,每次都未响应,用QtDesigner也无法启动 这个问题把我折磨了半天,最后才知道原来是要删除C:\Users\Administrat ...

  9. ArcGIS Runtime SDK是什么?

    如上图,Runtime SDK是什么东西?居然还有安卓.苹果手机.Mac.QT的版本? 是不是意味着ArcGIS的编辑数据和空间分析可以通过编程的方法在每个平台上满地跑了? 答案是:是,也不是. 1. ...

  10. bzoj 1566: [NOI2009]管道取珠

    Description   Input 第一行包含两个整数n, m,分别表示上下两个管道中球的数目. 第二行为一个AB字符串,长度为n,表示上管道中从左到右球的类型.其中A表示浅色球,B表示深色球. ...