1.Spring 框架的 AOP

2.Spring 中基于 AOP 的 XML架构

3.Spring 中基于 AOP 的 @AspectJ

4..AOP实际操作测试

5.注解配置aop实际操作

1.Spring 框架的 AOP

Spring 框架的一个关键组件是面向方面的编程(AOP)框架。面向方面的编程需要把程序逻辑分解成不同的部分称为所谓的关注点。跨一个应用程序的多个点的功能被称为横切关注点,这些横切关注点在概念上独立于应用程序的业务逻辑。有各种各样的常见的很好的方面的例子,如日志记录、审计、声明式事务、安全性和缓存等。

在 OOP 中,关键单元模块度是类,而在 AOP 中单元模块度是方面。依赖注入帮助你对应用程序对象相互解耦和 AOP 可以帮助你从它们所影响的对象中对横切关注点解耦。AOP 是像编程语言的触发物,如 Perl,.NET,Java 或者其他。

Spring AOP 模块提供拦截器来拦截一个应用程序,例如,当执行一个方法时,你可以在方法执行之前或之后添加额外的功能。

AOP 术语

在我们开始使用 AOP 工作之前,让我们熟悉一下 AOP 概念和术语。这些术语并不特定于 Spring,而是与 AOP 有关的。

项 描述
Aspect 一个模块具有一组提供横切需求的 APIs。例如,一个日志模块为了记录日志将被 AOP 方面调用。应用程序可以拥有任意数量的方面,这取决于需求。
Join point 在你的应用程序中它代表一个点,你可以在插件 AOP 方面。你也能说,它是在实际的应用程序中,其中一个操作将使用 Spring AOP 框架。
Advice 这是实际行动之前或之后执行的方法。这是在程序执行期间通过 Spring AOP 框架实际被调用的代码。
Pointcut 这是一组一个或多个连接点,通知应该被执行。你可以使用表达式或模式指定切入点正如我们将在 AOP 的例子中看到的。
Introduction 引用允许你添加新方法或属性到现有的类中。
Target object 被一个或者多个方面所通知的对象,这个对象永远是一个被代理对象。也称为被通知对象。
Weaving Weaving 把方面连接到其它的应用程序类型或者对象上,并创建一个被通知的对象。这些可以在编译时,类加载时和运行时完成。

通知的类型

Spring 方面可以使用下面提到的五种通知工作:

通知 描述
前置通知 在一个方法执行之前,执行通知。
后置通知 在一个方法执行之后,不考虑其结果,执行通知。
返回后通知 在一个方法执行之后,只有在方法成功完成时,才能执行通知。
抛出异常后通知 在一个方法执行之后,只有在方法退出抛出异常时,才能执行通知。
环绕通知 在建议方法调用之前和之后,执行通知。

实现自定义方面

Spring 支持 @AspectJ annotation style 的方法和基于模式的方法来实现自定义方面。这两种方法已经在下面两个子节进行了详细解释。

方法 描述
XML Schema based 方面是使用常规类以及基于配置的 XML 来实现的。
@AspectJ based @AspectJ 引用一种声明方面的风格作为带有 Java 5 注释的常规 Java 类注释。

2.Spring 中基于 AOP 的 XML架构

为了在本节的描述中使用 aop 命名空间标签,你需要导入 spring-aop j架构,如下所述:

<?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: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 "> <!-- bean definition & AOP specific configuration --> </beans>

你还需要在你的应用程序的 CLASSPATH 中使用以下 AspectJ 库文件。这些库文件在一个 AspectJ 装置的 ‘lib’ 目录中是可用的,否则你可以在 Internet 中下载它们。

  • aspectjrt.jar

  • aspectjweaver.jar

  • aspectj.jar

  • aopalliance.jar

     声明一个 aspect

    一个 aspect 是使用 元素声明的,支持的 bean 是使用 ref 属性引用的,如下所示:

    <aop:config>
    <aop:aspect id="myAspect" ref="aBean">
    ...
    </aop:aspect>
    </aop:config>
    <bean id="aBean" class="...">
    ...
    </bean>

    声明一个切入点

    一个切入点有助于确定使用不同建议执行的感兴趣的连接点(即方法)。在处理基于配置的 XML 架构时,切入点将会按照如下所示定义

    <aop:config>
    <aop:aspect id="myAspect" ref="aBean">
    <aop:pointcut id="businessService"
    expression="execution(* com.xyz.myapp.service.*.*(..))"/>
    ...
    </aop:aspect>
    </aop:config>
    <bean id="aBean" class="...">
    ...
    </bean>

    声明建议

    你可以使用 <aop:{ADVICE NAME}> 元素在一个 中声明五个建议中的任何一个,如下所示:

    <aop:config>
    <aop:aspect id="myAspect" ref="aBean">
    <aop:pointcut id="businessService"
    expression="execution(* com.xyz.myapp.service.*.*(..))"/>
    <!-- a before advice definition -->
    <aop:before pointcut-ref="businessService"
    method="doRequiredTask"/>
    <!-- an after advice definition -->
    <aop:after pointcut-ref="businessService"
    method="doRequiredTask"/>
    <!-- an after-returning advice definition -->
    <!--The doRequiredTask method must have parameter named retVal -->
    <aop:after-returning pointcut-ref="businessService"
    returning="retVal"
    method="doRequiredTask"/>
    <!-- an after-throwing advice definition -->
    <!--The doRequiredTask method must have parameter named ex -->
    <aop:after-throwing pointcut-ref="businessService"
    throwing="ex"
    method="doRequiredTask"/>
    <!-- an around advice definition -->
    <aop:around pointcut-ref="businessService"
    method="doRequiredTask"/>
    ...
    </aop:aspect>
    </aop:config>
    <bean id="aBean" class="...">
    ...
    </bean>

    你可以对不同的建议使用相同的 doRequiredTask 或者不同的方法。这些方法将会作为 aspect 模块的一部分来定义。

    这里是 Logging.java 文件的内容。这实际上是 aspect 模块的一个示例,它定义了在各个点调用的方法。

    package com.tutorialspoint;
    public class Logging {
    /**
    * This is the method which I would like to execute
    * before a selected method execution.
    */
    public void beforeAdvice(){
    System.out.println("Going to setup student profile.");
    }
    /**
    * This is the method which I would like to execute
    * after a selected method execution.
    */
    public void afterAdvice(){
    System.out.println("Student profile has been setup.");
    }
    /**
    * This is the method which I would like to execute
    * when any method returns.
    */
    public void afterReturningAdvice(Object retVal){
    System.out.println("Returning:" + retVal.toString() );
    }
    /**
    * This is the method which I would like to execute
    * if there is an exception raised.
    */
    public void AfterThrowingAdvice(IllegalArgumentException ex){
    System.out.println("There has been an exception: " + ex.toString());
    }
    }

    下面是 Student.java 文件的内容:

    package com.tutorialspoint;
    public class Student {
    private Integer age;
    private String name;
    public void setAge(Integer age) {
    this.age = age;
    }
    public Integer getAge() {
    System.out.println("Age : " + age );
    return age;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getName() {
    System.out.println("Name : " + name );
    return name;
    }
    public void printThrowException(){
    System.out.println("Exception raised");
    throw new IllegalArgumentException();
    }
    }

    下面是 MainApp.java 文件的内容:

    package com.tutorialspoint;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    public class MainApp {
    public static void main(String[] args) {
    ApplicationContext context =
    new ClassPathXmlApplicationContext("Beans.xml");
    Student student = (Student) context.getBean("student");
    student.getName();
    student.getAge();
    student.printThrowException();
    }
    }

    下面是配置文件 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: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 "> <aop:config>
    <aop:aspect id="log" ref="logging">
    <aop:pointcut id="selectAll"
    expression="execution(* com.tutorialspoint.*.*(..))"/>
    <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
    <aop:after pointcut-ref="selectAll" method="afterAdvice"/>
    <aop:after-returning pointcut-ref="selectAll"
    returning="retVal"
    method="afterReturningAdvice"/>
    <aop:after-throwing pointcut-ref="selectAll"
    throwing="ex"
    method="AfterThrowingAdvice"/>
    </aop:aspect>
    </aop:config> <!-- Definition for student bean -->
    <bean id="student" class="com.tutorialspoint.Student">
    <property name="name" value="Zara" />
    <property name="age" value="11"/>
    </bean> <!-- Definition for logging aspect -->
    <bean id="logging" class="com.tutorialspoint.Logging"/> </beans>

    一旦你已经完成的创建了源文件和 bean 配置文件,让我们运行一下应用程序。如果你的应用程序一切都正常的话,这将会输出以下消息:

    Going to setup student profile.
    Name : Zara
    Student profile has been setup.
    Returning:Zara
    Going to setup student profile.
    Age : 11
    Student profile has been setup.
    Returning:11
    Going to setup student profile.
    Exception raised
    Student profile has been setup.
    There has been an exception: java.lang.IllegalArgumentException
    .....
    other exception content

    让我们来解释一下上面定义的在 com.tutorialspoint 中 选择所有方法的 。让我们假设一下,你想要在一个特殊的方法之前或者之后执行你的建议,你可以通过替换使用真实类和方法名称的切入点定义中的星号(*)来定义你的切入点来缩短你的执行。

    <?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: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 "> <aop:config>
    <aop:aspect id="log" ref="logging">
    <aop:pointcut id="selectAll"
    expression="execution(* com.tutorialspoint.Student.getName(..))"/>
    <aop:before pointcut-ref="selectAll" method="beforeAdvice"/>
    <aop:after pointcut-ref="selectAll" method="afterAdvice"/>
    </aop:aspect>
    </aop:config> <!-- Definition for student bean -->
    <bean id="student" class="com.tutorialspoint.Student">
    <property name="name" value="Zara" />
    <property name="age" value="11"/>
    </bean> <!-- Definition for logging aspect -->
    <bean id="logging" class="com.tutorialspoint.Logging"/> </beans>

    3.Spring 中基于 AOP 的 @AspectJ

    @AspectJ 作为通过 Java 5 注释注释的普通的 Java 类,它指的是声明 aspects 的一种风格。通过在你的基于架构的 XML 配置文件中包含以下元素,@AspectJ 支持是可用的。

    <aop:aspectj-autoproxy/>

    你还需要在你的应用程序的 CLASSPATH 中使用以下 AspectJ 库文件。这些库文件在一个 AspectJ 装置的 ‘lib’ 目录中是可用的,如果没有,你可以在 Internet 中下载它们。

    • aspectjrt.jar

    • aspectjweaver.jar

    • aspectj.jar

    • aopalliance.jar
      <!--使用AspectJ方式注解需要相应的包-->
      <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.6.11</version>
      </dependency>
      <!--使用AspectJ方式注解需要相应的包-->
      <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.6.11</version>
      </dependency>

     声明一个 aspect

    Aspects 类和其他任何正常的 bean 一样,除了它们将会用 @AspectJ 注释之外,它和其他类一样可能有方法和字段,如下所示:

    package org.xyz;
    import org.aspectj.lang.annotation.Aspect;
    @Aspect
    public class AspectModule {
    }

    它们将在 XML 中按照如下进行配置,就和其他任何 bean 一样:

    <bean id="myAspect" class="org.xyz.AspectModule">
    <!-- configure properties of aspect here as normal -->
    </bean>

    声明一个切入点

    一个切入点有助于确定使用不同建议执行的感兴趣的连接点(即方法)。在处理基于配置的 XML 架构时,切入点的声明有两个部分:

    • 一个切入点表达式决定了我们感兴趣的哪个方法会真正被执行。

    • 一个切入点标签包含一个名称和任意数量的参数。方法的真正内容是不相干的,并且实际上它应该是空的。

    下面的示例中定义了一个名为 ‘businessService’ 的切入点,该切入点将与 com.tutorialspoint 包下的类中可用的每一个方法相匹配:

    import org.aspectj.lang.annotation.Pointcut;
    @Pointcut("execution(* com.xyz.myapp.service.*.*(..))") // expression
    private void businessService() {} // signature

    下面的示例中定义了一个名为 ‘getname’ 的切入点,该切入点将与 com.tutorialspoint 包下的 Student 类中的 getName() 方法相匹配:

    import org.aspectj.lang.annotation.Pointcut;
    @Pointcut("execution(* com.tutorialspoint.Student.getName(..))")
    private void getname() {}

    声明建议

    你可以使用 @{ADVICE-NAME} 注释声明五个建议中的任意一个,如下所示。这假设你已经定义了一个切入点标签方法 businessService():

    @Before("businessService()")
    public void doBeforeTask(){
    ...
    }
    @After("businessService()")
    public void doAfterTask(){
    ...
    }
    @AfterReturning(pointcut = "businessService()", returning="retVal")
    public void doAfterReturnningTask(Object retVal){
    // you can intercept retVal here.
    ...
    }
    @AfterThrowing(pointcut = "businessService()", throwing="ex")
    public void doAfterThrowingTask(Exception ex){
    // you can intercept thrown exception here.
    ...
    }
    @Around("businessService()")
    public void doAroundTask(){
    ...
    }

    你可以为任意一个建议定义你的切入点内联。下面是在建议之前定义内联切入点的一个示例:

    @Before("execution(* com.xyz.myapp.service.*.*(..))")
    public doBeforeTask(){
    ...
    }

    详见:https://www.w3cschool.cn/wkspring/k4q21mm8.html

4.AOP实际操作测试:

一。这实际上是 aspect 模块的一个示例,它定义了在各个点调用的方法。

package com.my.dm.model;

import org.aspectj.lang.ProceedingJoinPoint;

public class Logging {

    private static final int DEFAULT_MAX_RETRIES =2;
private int maxRetries = DEFAULT_MAX_RETRIES;
/**
* This is the method which I would like to execute
* before a selected method execution.
*/
public void beforeAdvice(){
System.out.println("before advice");
}
/**
* This is the method which I would like to execute
* after a selected method execution.
*/
public void afterAdvice(){
System.out.println("after advice");
}
/**
* This is the method which I would like to execute
* when any method returns.
*/
public void afterReturningAdvice(Object returnVal){
System.out.println("Returning:" + returnVal.toString() );
}
/**
* This is the method which I would like to execute
* if there is an exception raised.
*/
public void afterThrowingAdvice(IllegalArgumentException ex){
System.out.println("There has been an exception: " + ex.toString());
} /**
* This is the method which I would like to execute
* around a selected method execution.
* @throws Throwable
*/
public Object aroundAdvice1(ProceedingJoinPoint pjp) throws Throwable{
//环绕通知处理方法
int numAttempts = 0;
Exception lockFailureException; do {
numAttempts++;
try {
System.out.println("环绕通知方法[ doConcurrentOperation(ProceedingJoinPoint pjp) ].............");
return pjp.proceed();
}
catch(Exception ex) {
lockFailureException = ex;
}
}
while(numAttempts <= this.maxRetries);
throw lockFailureException;
} //简单环绕通知处理方法
public Object aroundAdvice2(ProceedingJoinPoint pjp) throws Throwable{
//环绕通知处理方法
return pjp.proceed();
} } 

二.实体类

package com.my.dm.model;

import org.springframework.beans.factory.annotation.Required;

public class Person{

    private String perId;

    private String perName;

    private int perAge;

    public Person() {
super();
System.out.println("====Use constructor which has 0 parameter!"); } public Person(String perId, String perName, int perAge) {
super();
this.perId = perId;
this.perName = perName;
this.perAge = perAge;
System.out.println("====Use constructor which has 3 parameter!");
} /**
* @return the perId
*/
public String getPerId() {
return perId;
} /**
* @param perId the perId to set
*/
public void setPerId(String perId) {
this.perId = perId;
} /**
* @return the perName
*/
public String getPerName() {
return perName;
} /**
* @param perName the perName to set
*/
public void setPerName(String perName) {
this.perName = perName;
} /**
* @return the perAge
*/
public int getPerAge() {
return perAge;
} /**
* @param perAge the perAge to set
*/
@Required
public void setPerAge(int perAge) {
this.perAge = perAge;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/ @Override
public String toString() {
return "Person [perId=" + perId + ", perName=" + perName + ", perAge=" + perAge + "]";
} public String getStr() {
// TODO Auto-generated method stub
return "This is a test!";
} public void raiseException() {
// TODO Auto-generated method stub throw new IllegalArgumentException(); } }

三.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: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 "> <!--init person -->
<bean id="person" class="com.my.dm.model.Person">
<property name="perId" value="AA"></property>
<property name="perName" value="gril"></property>
<property name="perAge" value="23"></property>
</bean>
<!-- <bean id="PersonEventHandler1" class="com.my.dm.bean.test.PersonEventHandler1"
/> <bean id="PersonEventHandler2" class="com.my.dm.bean.test.PersonEventHandler2"
/> -->
<aop:config>
<aop:aspect id="myAspect" ref="logging">
<aop:pointcut id="businessService"
expression="execution(* com.my.dm.model.Person.getPerAge(..))" />
<aop:pointcut id="aroundBusinessService"
expression="execution(* com.my.dm.model.Person.getStr(..))" />
<!-- a before advice definition -->
<aop:before pointcut-ref="businessService" method="beforeAdvice" />
<!-- an after advice definition -->
<aop:after pointcut-ref="businessService" method="afterAdvice" /> <!-- an after-returning advice definition -->
<!--The doRequiredTask method must have parameter named returnVal --> <aop:after-returning pointcut-ref="businessService"
returning="returnVal" method="afterReturningAdvice" /> <!-- an after-throwing advice definition -->
<!--The doRequiredTask method must have parameter named ex -->
<aop:after-throwing pointcut-ref="businessService" throwing="ex"
method="afterThrowingAdvice"/> <!-- an around advice definition -->
<!-- 需要处理ProceedingJoinPoint类型参数 -->
<aop:around pointcut-ref="aroundBusinessService" method="aroundAdvice2"/> </aop:aspect>
</aop:config> <!-- Definition for logging aspect -->
<bean id="logging" class="com.my.dm.model.Logging" /> </beans>

四.spring aop 环绕通知around和其他通知的区别

前言:

spring 的环绕通知和前置通知,后置通知有着很大的区别,主要有两个重要的区别:

1) 目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,而前置和后置通知   是不能决定的,他们只是在方法的调用前后执行通知而已,即目标方法肯定是要执行的。

2)  环绕通知可以控制返回对象,即你可以返回一个与目标对象完全不同的返回值,虽然这很危险,但是你却可以办到。而后置方法是无法办到的,因为他是在目标方法返回值后调用

这里是经过我自己测试的过的例子,使用面向切面来处理一些问公共的问题,比如,权限管理,事务的委托

五。Spring AOP 中@Pointcut的用法

格式:

execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?) 

括号中各个pattern分别表示:

  • 修饰符匹配(modifier-pattern?)
  • 返回值匹配(ret-type-pattern)可以为*表示任何返回值,全路径的类名等
  • 类路径匹配(declaring-type-pattern?)
  • 方法名匹配(name-pattern)可以指定方法名 或者 *代表所有, set* 代表以set开头的所有方法
  • 参数匹配((param-pattern))可以指定具体的参数类型,多个参数间用“,”隔开,各个参数也可以用“*”来表示匹配任意类型的参数,如(String)表示匹配一个String参数的方法;(*,String) 表示匹配有两个参数的方法,第一个参数可以是任意类型,而第二个参数是String类型;可以用(..)表示零个或多个任意参数
  • 异常类型匹配(throws-pattern?)
  • 其中后面跟着“?”的是可选项
    1)execution(* *(..))
    //表示匹配所有方法
    2)execution(public * com. savage.service.UserService.*(..))
    //表示匹配com.savage.server.UserService中所有的公有方法
    3)execution(* com.savage.server..*.*(..))
    //表示匹配com.savage.server包及其子包下的所有方法
    execution(public String org.baeldung.dao.FooDao.findById(Long))

    within

  • 在Spring 2.0中,Pointcut的定义包括两个部分:Pointcut表示式(expression)和Pointcut签名(signature)
    //Pointcut表示式
    @Pointcut("execution(* com.savage.aop.MessageSender.*(..))")
    //Point签名
    private void log(){}
    然后要使用所定义的Pointcut时,可以指定Pointcut签名
  • @Before("log()")
  • 使用within切点批示符可以达到上面例子一样的效果,within用来限定连接点属于某个确定类型的类。如下面代码的效果与下面的例子是一样的:

    @Pointcut("within(org.baeldung.dao.FooDao)")
    @Pointcut("execution(* org.baeldung.dao.FooDao.*(..))")
  • 我们也可以使用within指示符来匹配某个包下面所有类的方法(包括子包下面的所有类方法),如下代码所示:

  • @Pointcut("within(org.baeldung..*)")
  • this 和 target

    this用来匹配的连接点所属的对象引用是某个特定类型的实例,target用来匹配的连接点所属目标对象必须是指定类型的实例;那么这两个有什么区别呢?原来AspectJ在实现代理时有两种方式:
    1、如果当前对象引用的类型没有实现自接口时,spring aop使用生成一个基于CGLIB的代理类实现切面编程
    2、如果当前对象引用实现了某个接口时,Spring aop使用JDK的动态代理机制来实现切面编程
    this指示符就是用来匹配基于CGLIB的代理类,通俗的来讲就是,如果当前要代理的类对象没有实现某个接口的话,则使用this;target指示符用于基于JDK动态代理的代理类,通俗的来讲就是如果当前要代理的目标对象有实现了某个接口的话,则使用target.:

    public class FooDao implements BarDao {
    ...
    }

    比如在上面这段代码示例中,spring aop将使用jdk的动态代理来实现切面编程,在编写匹配这类型的目标对象的连接点表达式时要使用target指示符, 如下所示:

    @Pointcut("target(org.baeldung.dao.BarDao)")

    如果FooDao类没有实现任何接口,或者在spring aop配置属性:proxyTargetClass设为true时,Spring Aop会使用基于CGLIB的动态字节码技为目标对象生成一个子类将为代理类,这时应该使用this指示器:

    @Pointcut("this(org.baeldung.dao.FooDao)")

    参数

    参数指示符是一对括号所括的内容,用来匹配指定方法参数:


    @Pointcut("execution(* *..find*(Long))")

    这个切点匹配所有以find开头的方法,并且只一个Long类的参数。如果我们想要匹配一个有任意个参数,但是第一个参数必须是Long类的,我们这可使用下面这个切点表达式:

    @Pointcut("execution(* *..find*(Long,..))")

    @Target

    这个指示器匹配指定连接点,这个连接点所属的目标对象的类有一个指定的注解:

    @Pointcut("@target(org.springframework.stereotype.Repository)")

    @args

    这个指示符是用来匹配连接点的参数的,@args指出连接点在运行时传过来的参数的类必须要有指定的注解,假设我们希望切入所有在运行时接受实@Entity注解的bean对象的方法:

    @Pointcut("@args(org.baeldung.aop.annotations.Entity)")
    public void methodsAcceptingEntities() {}

    为了在切面里接收并使用这个被@Entity的对象,我们需要提供一个参数给切面通知:JointPoint:

    @Before("methodsAcceptingEntities()")
    public void logMethodAcceptionEntityAnnotatedBean(JoinPoint jp) {
    logger.info("Accepting beans with @Entity annotation: " + jp.getArgs()[0]);
    }

    @within

    这个指示器,指定匹配必须包括某个注解的的类里的所有连接点:

    @Pointcut("@within(org.springframework.stereotype.Repository)")

    上面的切点跟以下这个切点是等效的:

    @Pointcut("within(@org.springframework.stereotype.Repository *)")

    @annotation

    这个指示器匹配那些有指定注解的连接点,比如,我们可以新建一个这样的注解@Loggable:

    @Pointcut("@annotation(org.baeldung.aop.annotations.Loggable)")
    public void loggableMethods() {}

    我们可以使用@Loggable注解标记哪些方法执行需要输出日志:

    @Before("loggableMethods()")
    public void logMethod(JoinPoint jp) {
    String methodName = jp.getSignature().getName();
    logger.info("Executing method: " + methodName);
    }

    切点表达式组合

    可以使用&&、||、!、三种运算符来组合切点表达式,表示与或非的关系。

    @Pointcut("@target(org.springframework.stereotype.Repository)")
    public void repositoryMethods() {} @Pointcut("execution(* *..create*(Long,..))")
    public void firstLongParamMethods() {} @Pointcut("repositoryMethods() && firstLongParamMethods()")
    public void entityCreationMethods() {}

    作者的github例子:https://github.com/eugenp/tutorials/tree/master/spring-mvc-java

  • 5.注解配置aop实际操作

10.Spring——框架的AOP的更多相关文章

  1. Spring框架之AOP源码完全解析

    Spring框架之AOP源码完全解析 Spring可以说是Java企业开发里最重要的技术.Spring两大核心IOC(Inversion of Control控制反转)和AOP(Aspect Orie ...

  2. Spring框架的AOP技术(注解方式)

    1. 步骤一:创建JavaWEB项目,引入具体的开发的jar包 * 先引入Spring框架开发的基本开发包 * 再引入Spring框架的AOP的开发包 * spring的传统AOP的开发的包 * sp ...

  3. Spring 框架的 AOP 简介

    Spring 框架的 AOP Spring 框架的一个关键组件是面向方面的编程(AOP)(也称为面向切面编程)框架. 面向方面的编程需要把程序逻辑分解成不同的部分称为所谓的关注点. 跨一个应用程序的多 ...

  4. 使用spring框架进行aop编程实现方法调用前日志输出

    aop编程 之使用spring框架实现方法调用前日志输出 使用spring框架实现AOP编程首先需要搭建spring框架环境: 使用Spring框架实现AOP工程编程之后,不需要我们去写代理工厂了,工 ...

  5. Spring 框架的AOP之注解的方式

    1. 环境搭建 1.1 导入 jar 包 Spring 框架的基本开发包(6个); Spring 的传统AOP的开发包 spring-aop-4.3.10.RELEASE org.aopallianc ...

  6. Spring框架(4)---AOP讲解铺垫

    AOP讲解铺垫      不得不说,刚开始去理解这个Aop是有点难理解的,主要还是新的概念比较多,对于初学者一下子不一定马上能够快速吸收,所以我先对什么事Aop做一个解释: 首先说明:本文不是自己所写 ...

  7. Spring框架的AOP的底层实现

    1. Srping框架的AOP技术底层也是采用的代理技术,代理的方式提供了两种 1. 基于JDK的动态代理 * 必须是面向接口的,只有实现了具体接口的类才能生成代理对象 2. 基于CGLIB动态代理 ...

  8. Spring框架及AOP

    Spring核心概念 Spring框架大约由20个功能模块组成,这些模块主分为六个部分: Core Container :基础部分,提供了IoC特性. Data Access/Integration ...

  9. 10 Spring框架--基于注解和xml的配置的应用案例

    1.项目结构 2.基于xml配置的项目 <1>账户的业务层接口及其实现类 IAccountService.java package lucky.service; import lucky. ...

随机推荐

  1. VMware workstation 11 的下载

    不多说,直接上干货! VMWare Workstation 11的下载详细: 谷歌FQ,进入. 具体怎么达到可以FQ,见 FQ软件lantern-installer-beta.exe(推荐) 成功! ...

  2. Python -- Gui编程 -- Win32API的使用

    消息框 messageBox.py import win32api, win32con win32api.MessageBox(0, 'Hello World!', 'Come Here', win3 ...

  3. 纯C++安卓开发 (ndk)系列之 ---- 常见问题

    常见问题1:run as Android Application运行时提示无法识别到模拟器 解决步骤如下: (1)首先查看安卓模拟器是否已经打开 (2)如果安卓模拟器已经打开,则操作步骤为:点击Ecl ...

  4. Eth 部署智能合约

    首先要开发以太坊的智能合约,需要EVM(以太坊虚拟机),也就是需要运行的环境,我们可以通过 geth 来设置开发环境: geth --datadir testNet --dev console 2&g ...

  5. openTSDB(转)

    1.OpenTSDB介绍 1.1.OpenTSDB是什么?主要用途是什么? 官方文档这样描述:OpenTSDB is a distributed, scalable Time Series Datab ...

  6. JDK 泛型

    JDK1.5 令我们期待很久,可是当他发布的时候却更换版本号为5.0.这说明Java已经有大幅度的变化.本文将讲解JDK5.0支持的新功能-----Java的泛型. 1.Java泛型 其实Java的泛 ...

  7. [转]Configure Network Drive Visible for SQL Server During Backup and Restore Using SSMS

    本文转自:https://mytechmantra.com/LearnSQLServer/Configure-Network-Drive-Visible-for-SQL-Server-During-B ...

  8. dev控件ASPxComboBox设置ReadOnly="true"后

    dev控件ASPxComboBox设置ReadOnly="true"后,在后台OnCallback事件中赋值前台不显示

  9. Angular待办事项应用3

    隔离业务逻辑 接着上一节,业务逻辑应该复古牛仔单独的service中,我们在todo文件夹中建立TodoService ng g s todo/todo 引入UUID包 todo中id要唯一,一个是采 ...

  10. [日常] Linux使用diff来比较目录

    Linux diff比较两个目录的不同: diff dir1 dir2  -urNaq -a  --text  Treat all files as text. -u  -U NUM  --unifi ...