欢迎使用CSDN-markdown编辑器

来源:互联网 发布:淘宝如何开天猫店 编辑:程序博客网 时间:2024/06/05 16:16

前期基础概念:
连接点(Joinpoint):程序执行过程中明确的点,如方法的调用或特定的异常被抛出。
切入点(Pointcut): 指定一个通知将被引发的一系列连接点的集合。AOP框架必须允许开发者指定切入点:例如,使用正则表达式。
通知(Advice): 在特定的连接点,AOP框架执行的动作。各种类型的通知包括“around”、“before”和“throws”通知。通知类型将在下面讨论。许多AOP框架 包括Spring都是以拦截器做通知模型,维护一个“围绕”连接点的拦截器链。
常见的通知类型:
前置通知,后置通知,返回通知,异常通知和环绕通知。
前置通知:在调用目标方法之前调用。
抛出异常可以组织后续代码执行。
后置通知:在调用目标方法之后调用。
目标方法正常执行完毕才会执行(抛出异常也会执行),但不可调用目标方法的返回值。
返回通知:在目标方法执行完毕之后调用。
目标方法正常执行完毕才会执行(抛出异常则不再执行),可调用目标方法的返回值。
异常通知:在目标方法抛出异常时调用。
环绕通知:在目标方法执行前后都会执行。
伪代码:
try{
调用前置通知
环绕前置处理
调用目标对象方法
环绕后置处理
调用返回通知
}catch(Exception e){
调用异常通知
}finally{
调用后置通知
}
代码示例:
需要的jar包:
aopalliance.jar
commons-logging-1.1.1.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
aspectj-1.8.10_1.jar
该示例是一个java工程,并非web工程
1.注解方式:
配置文件:aspect-annotion.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"    xmlns:context="http://www.springframework.org/schema/context"    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-4.0.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">        <- spring注解扫描 ->    <context:component-scan base-package="spring_aspect_annotation"></context:component-scan>               <- aspectj注解扫描,使@Aspect、@Pointcut注解生效 ->     <aop:aspectj-autoproxy/></beans>

接口文件:
该接口目标方法的接口,也可以省略接口直接写实现类

package spring_aspect_annotation;public interface Animal {       void eat();    void sleep();   }

该接口是通知的内容的接口

package spring_aspect_annotation;import org.aspectj.lang.JoinPoint;public interface Hello {        void sayHello(JoinPoint joinPoint); }

实现类:
通知的实现文件:

package spring_aspect_annotation.impl;import org.springframework.stereotype.Component;import spring_aspect_annotation.Animal;@Componentpublic class Cat implements Animal {    public void eat() {        System.out.println("eat-eat-eat-eat-eat-eat-eat");    }    public void sleep() {        System.out.println("sleep-sleep-sleep-sleep-sleep");    }}

目标方法的实现文件:

package spring_aspect_annotation.impl;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;import spring_aspect_annotation.Hello;@Aspect@Component//声明该类由是spring的一个组件,相当于在spring配置文件中配置bean标签public class HelloCat implements Hello {        @Pointcut("execution(* spring_aspect_annotation.Animal.*(..))")    //声明该方法是一个切入点,用来声明目标方法    public void declareJoinPointExpression(){}      @After("declareJoinPointExpression()")//声明通知的类型    public void sayHello(JoinPoint joinPoint) {     System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");                                           }}

测试文件:

package spring_aspect_annotation;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.stereotype.Component;public class Test {    public static void main(String[] args) {                ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("aspect-annotion.xml");        Animal animal = (Animal) applicationContext.getBean("cat");        animal.eat();        applicationContext.close();         }}

配置文件方式的通知配置:

<?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"    xmlns:context="http://www.springframework.org/schema/context"    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-4.0.xsd        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">        <bean id="helloWorldImpl1" class="com.aspectj.impl.HelloWorldImpl1" />            <bean id="helloWorldImpl2" class="com.aspectj.impl.HelloWorldImpl2" />              <bean id="timeHandler" class="com.aspectj.impl.TimeHandler" />                  <aop:config>                <aop:pointcut id="addAllMethod" expression="execution(* com.aspectj.HelloWorld.*(..))" />                   <aop:aspect id="time" ref="timeHandler">                       <aop:before method="printTime" pointcut-ref="addAllMethod" />                       <aop:after method="printTime" pointcut-ref="addAllMethod" />                   </aop:aspect>               </aop:config>    </beans>

最后这个配置文件跟上面注解实现的不是一个项目。现在大概讲解一下。
首先,需要配置文件实现的时候,就可以将各种类上的各种注解暂且先去掉。但是需要将目标方法的实现类和通知的实现类都在spring的IOC容器中进行配置
其次,进行spring AOP切面的配置。
切入点配置:需要制定一个id,因为在设置五种通知类型时,需要引用该切入点制定目标方法

<aop:pointcut id="addAllMethod" expression="execution(* com.aspectj.HelloWorld.*(..))" />

通知类型配置:

    <aop:aspect id="time" ref="timeHandler">                       <aop:before method="printTime" order="1" pointcut-ref="addAllMethod" />                       <aop:after method="printTime" pointcut-ref="addAllMethod" />                   </aop:aspect>

ref:指定引用的通知类。
method:指定在此处引用的通知类的对应方法。此处为其方法名。
pointcut-ref:指定引用的切入点。即目标方法。
order:指定通知的执行顺序。数值越小,越先执行。
注:
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(“aspect-annotion.xml”)
这句代码可以直接使用
ClassPathXmlApplicationContext 的接口ApplicationContext直接定义。因为在ConfigurableApplicationContext这个接口中定义了ApplicationContext释放资源
的close()方法。所以我在这里直接使用了ClassPathXmlApplicationContext 进行了applicationContext的创建。

Pointcut表达式
任意公共方法的执行:
execution(public * *(..))
任何一个名字以“set”开始的方法的执行:
execution(* set*(..))
AccountService接口定义的任意方法的执行:
execution(* com.xyz.service.AccountService.*(..))
在service包中定义的任意方法的执行:
execution(* com.xyz.service..(..))
在service包或其子包中定义的任意方法的执行:
execution(* com.xyz.service...(..))
在service包中的任意连接点(在Spring AOP中只是方法执行):
within(com.xyz.service.*)
在service包或其子包中的任意连接点(在Spring AOP中只是方法执行):
within(com.xyz.service..*)
实现了AccountService接口的代理对象的任意连接点 (在Spring AOP中只是方法执行):
this(com.xyz.service.AccountService)
‘this’在绑定表单中更加常用:- 请参见后面的通知一节中了解如何使得代理对象在通知体内可用。
实现AccountService接口的目标对象的任意连接点 (在Spring AOP中只是方法执行):
target(com.xyz.service.AccountService)
‘target’在绑定表单中更加常用:- 请参见后面的通知一节中了解如何使得目标对象在通知体内可用。
任何一个只接受一个参数,并且运行时所传入的参数是Serializable 接口的连接点(在Spring AOP中只是方法执行)
args(java.io.Serializable)
‘args’在绑定表单中更加常用:- 请参见后面的通知一节中了解如何使得方法参数在通知体内可用。
请注意在例子中给出的切入点不同于 execution(* *(java.io.Serializable)): args版本只有在动态运行时候传入参数是Serializable时才匹配,而execution版本在方法签名中声明只有一个 Serializable类型的参数时候匹配。
目标对象中有一个 @Transactional 注解的任意连接点 (在Spring AOP中只是方法执行)
@target(org.springframework.transaction.annotation.Transactional)
‘@target’在绑定表单中更加常用:- 请参见后面的通知一节中了解如何使得注解对象在通知体内可用。
任何一个目标对象声明的类型有一个 @Transactional 注解的连接点 (在Spring AOP中只是方法执行):
@within(org.springframework.transaction.annotation.Transactional)
‘@within’在绑定表单中更加常用:- 请参见后面的通知一节中了解如何使得注解对象在通知体内可用。
任何一个执行的方法有一个 @Transactional 注解的连接点 (在Spring AOP中只是方法执行)
@annotation(org.springframework.transaction.annotation.Transactional)
‘@annotation’在绑定表单中更加常用:- 请参见后面的通知一节中了解如何使得注解对象在通知体内可用。
任何一个只接受一个参数,并且运行时所传入的参数类型具有@Classified 注解的连接点(在Spring AOP中只是方法执行)
@args(com.xyz.security.Classified)
‘@args’在绑定表单中更加常用:- 请参见后面的通知一节中了解如何使得注解对象在通知体内可用。
任何一个在名为’tradeService’的Spring bean之上的连接点 (在Spring AOP中只是方法执行):
bean(tradeService)
任何一个在名字匹配通配符表达式’*Service’的Spring bean之上的连接点 (在Spring AOP中只是方法执行):
bean(*Service)

原创粉丝点击