本文转自:https://www.cnblogs.com/leiOOlei/p/3709607.html

首先看个例子,如下

接口代码:

package com.lei.demo.aop.schema;

public interface IHello {
public void sayHello();
}

接口实现:

package com.lei.demo.aop.schema;

public class HelloService implements IHello {

    public void sayHello() {
System.out.println("-----Hello World!-----");
} }

接下来我们要实现AOP,即调用sayHello方法时切入通知。

1.      第一种方法aop:config中配置aop:pointcut和aop:aspect

定义一个切面支持类HelloAspect.java

package com.lei.demo.aop.schema;

/*
* 切面支持类
*/
public class HelloAspect { //前置通知
public void beforeAdvice() {
System.out.println("===========before advice");
} //后置最终通知
public void afterFinallyAdvice() {
System.out.println("===========after finally advice");
}
}

 

Xml配置:Spring-AOP-Schema.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.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <!-- 目标类 -->
<bean id="helloService" class="com.lei.demo.aop.schema.HelloService" /> <bean id="helloAspect" class="com.lei.demo.aop.schema.HelloAspect" /> <!-- 配置切面 -->
<!-- aop:advisor,是有顺序的,必须放在aop:pointcut之后 -->
<aop:config>
<aop:pointcut id="helloPointcut" expression="execution(* com.lei.demo.aop.schema..*.*(..))" /> <aop:aspect ref="helloAspect">
<!—以下使用了两种方法定义切入点 pointcut-ref和pointcut-->
<aop:before pointcut-ref="helloPointcut" method="beforeAdvice" />
<aop:after pointcut="execution(* com.lei.demo.aop.schema..*.*(..))" method="afterFinallyAdvice" />
</aop:aspect> </aop:config>
</beans>

以上配置中method="beforeAdvice"method="afterFinallyAdvice"对应helloAspect中的方法

测试,App.java

package com.lei.demo.aop.schema;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { private static ApplicationContext ctx; public static void main(String[] args) { ctx = new ClassPathXmlApplicationContext("Spring-AOP-Schema.xml"); IHello helloService = ctx.getBean("helloService",IHello.class); helloService.sayHello();
} }

运行App.java,结果如下:

===========before advice

-----Hello World!-----

===========after finally advice

2.      第二种方法aop:config中配置aop:pointcut和aop:advisor

实现org.aopalliance.intercept.MethodInvocation接口,

HelloAroundAdvice.java如下:

package com.lei.demo.aop.schema;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; public class HelloAroundAdvice implements MethodInterceptor { public Object invoke(MethodInvocation arg0) throws Throwable { System.out.println("++++++before advice");
arg0.proceed();
System.out.println("++++++after advice"); return null;
} }

Xml配置:Spring-AOP-Schema.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.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <!-- 目标类 -->
<bean id="helloService" class="com.lei.demo.aop.schema.HelloService" /> <bean id="helloArroundAdvice" class="com.lei.demo.aop.schema.HelloAroundAdvice" /> <!-- 配置切面 -->
<aop:config>
<aop:pointcut id="helloPointcut" expression="execution(* com.lei.demo.aop.schema..*.*(..))" />
<aop:advisor advice-ref="helloArroundAdvice" pointcut-ref="helloPointcut"/> </aop:config>
</beans>

仍然运行App.java,结果如下:

++++++before advice

-----Hello World!-----

++++++after advice

spring aop的两种写法aspect和advisor的更多相关文章

  1. spring AOP的两种代理

    本篇记录下spring AOP的两种代理,为下一篇AOP实现做下铺垫. 1.JDK动态代理  2.cglib代理 1.如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP2.如果目标对象 ...

  2. 使用aspectJ实现Spring AOP的两种方式

    方式一:基于aspectJ的XML配置 方式二:基于aspectJ的注解方式 基于aspectJ的XML配置 1)       引入相关jar包 2)       创建Spring核心配置文件,必须导 ...

  3. (一)spring aop的两种配置方式。

    sring aop的方式有两种:(1)xml文件配置方式(2)注解的方式实现,我们可以先通过一个demo认识spring aop的实现,然后再对其进行详细的解释. 一.基于注解的springAop配置 ...

  4. spring AOP的两种配置方式

    连接点(JoinPoint) ,就是spring允许你是通知(Advice)的地方,那可就真多了,基本每个方法的前.后(两者都有也行),或抛出异常是时都可以是连接点,spring只支持方法连接点.其他 ...

  5. spring ----> aop的两种实现方式

    实现1:基于xml package com.rr.spring3.interf; //接口 public interface SayHello { public void sayHello(); } ...

  6. spring AOP的两种配置

    xml配置 定义要被代理的方法的接口 public interface TestAop { public void print(String s); } 实现上述接口 public class Tes ...

  7. JAVA高级架构师基础功:Spring中AOP的两种代理方式:动态代理和CGLIB详解

    在spring框架中使用了两种代理方式: 1.JDK自带的动态代理. 2.Spring框架自己提供的CGLIB的方式. 这两种也是Spring框架核心AOP的基础. 在详细讲解上述提到的动态代理和CG ...

  8. springmvc配置AOP的两种方式

    spingmvc配置AOP有两种方式,一种是利用注解的方式配置,另一种是XML配置实现. 应用注解的方式配置: 先在maven中引入AOP用到的依赖 <dependency> <gr ...

  9. Spring管理事物两种方式

    Spring管理事物两种方式 1. 编程式事物管理(在开发中不经常使用) 使用步骤 1. 配置数据库事物管理 DataSourceTransactionManager <!--配置事物管理器-- ...

随机推荐

  1. 追MM和Java的23种设计模式

    我在Java论坛看到这篇文章,作者以轻松的语言比喻了java的32种模式,有很好的启发作用,但可惜没有给出具体的意思,我就在后边加上了.这些都是最简单的介绍,要学习的话建议你看一下阎宏博士的<J ...

  2. 文件操作篇 close creat dup dup2 fcntl flock fsync lseek mkstemp open read sync write

    文件操作篇 close creat dup dup2 fcntl flock fsync lseek mkstemp open read sync write close(关闭文件) 相关函数 ope ...

  3. Objective-C:在类中设置不同协议

    在下面的代码中,设置了两种不同的协议规则:一种是老师对学生设置的协议:即老师发出命令后,学生站起来.回答问题.坐下; 另一种是我对学生设置的协议:即学生按照我的协议中的初始化函数去初始化一个整数. / ...

  4. 如何判断一个文本文件内容的编码格式 UTF-8 ? ANSI(GBK)

    转自:http://blog.csdn.net/jiangqin115/article/details/42684017 UTF-8编码的文本文档,有的带有BOM (Byte Order Mark, ...

  5. strtok()函数

    strtok()这个函数大家都应该碰到过,但好像总有些问题, 这里着重讲下它 首先看下MSDN上的解释: char *strtok( char *strToken, const char *strDe ...

  6. 遇到Elements in iteration expect to have 'v-bind:key' directives.' 这个错误

    解决方式一:更改VS Code编辑器的vetur配置 错误提示: [vue-language-server] Elements in iteration expect to have 'v-bind: ...

  7. Informatica 常用组件Source Qualifier之七 使用排序端口

    使用已排序端口时,PowerCenter 将添加端口至默认查询中的 ORDER BY 子句.PowerCenter Server 将添加配置的端口号,从源限定符转换的顶部开始.在映射中包括以下任何转换 ...

  8. C++ vector 删除符合条件的元素

    C++ vector中实际删除元素使用的是容器vecrot中std::vector::erase()方法. C++ 中std::remove()并不删除元素,因为容器的size()没有变化,只是元素的 ...

  9. iOS开发-DatePicker控件

    时间控件不管是Android还是iOS中都是必然存在的一个控件,具体的效果大同小异,显示日期,时间,iOS中有四种方式可以选择,Time, Date,Date and Time  , Count Do ...

  10. android.net.Uri 简介 API

    android.net.Uri 简介 public abstract class android.net.Uri extends Object implements Parcelable, Compa ...