1.前置通知

接口:

package chapter1.server;

public interface IHelloService {

	public void sayAdvisorBefore(String param) ;
}

  实现

package chapter1.service.impl;

import chapter1.server.IHelloService;

public class HelloService implements IHelloService {	

	public void sayAdvisorBefore(String param) {
// TODO Auto-generated method stub
System.out.println("============say " + param);
} }

  配置:

<!-- 启动对Aspectj的支持 -->
<aop:aspectj-autoproxy/>
<bean id="helloService" class="chapter1.service.impl.HelloService" />
<bean id="aspect" class="chapter1.aop.HelloAspect"/>

  aop:

package chapter1.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut; @Aspect
public class HelloAspect { //定义切入点
@Pointcut(value="execution(* chapter1..*.sayAdvisorBefore(java.lang.String)) && args(param)", argNames = "param")
public void beforePointcut(String param) {} //定义通知
@Before(value = "beforePointcut(param)", argNames = "param")
public void beforeAdvice(String param) {
System.out.println("===========before advice param:" + param);
}
}

  测试程序:

//前置通知
public void testAspectj()
{
ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj.xml");
IHelloService hello = context.getBean("helloService", IHelloService.class);
hello.sayAdvisorBefore("before");
}

  

2.后置返回通知

接口

package chapter1.server;

public interface IHelloService2 {
public int sayAfterReturning(String param);
}

  实现

package chapter1.service.impl;

import chapter1.server.IHelloService2;

public class HelloService2 implements IHelloService2 {

	public int sayAfterReturning(String param) {
// TODO Auto-generated method stub
System.out.println("============ say after returning:" + param);
return 1;
} }

  配置:

<aop:aspectj-autoproxy/>
<bean id="helloService" class="chapter1.service.impl.HelloService2" />
<bean id="aspect" class="chapter1.aop.HelloAspect2"/>

  aop:

package chapter1.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.AfterReturning; @Aspect
public class HelloAspect2 { //方法一
//通知
@AfterReturning(
//value="execution(* chapter1..*.sayAdvisorBefore(java.lang.String)) and args(param)",
value="execution(* chapter1..*.sayAfterReturning(..))",
argNames="retVal",
returning="retVal")
public void afterReturningAdvice(Object retVal)
{
System.out.println("================= return after advice : " + retVal);
} //方法二
//定义切入点
@Pointcut(value="execution(* chapter1..*.sayAfterReturning(java.lang.String) and args(param))", argNames="param")
public void returnPointcut(String param) {} public void afterReturningAdvice2(Object retVal)
{ } }

  测试程序:

//后置返回通知
public void testAspectAfterReturning()
{
ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj2.xml");
IHelloService2 hello = context.getBean("helloService", IHelloService2.class);
hello.sayAfterReturning("hahah");
}

  

3.后置错误通知

接口

package chapter1.server;

public interface IHelloService3 {
public boolean sayAfterThrow(String param);
}

  实现:

package chapter1.service.impl;

import chapter1.server.IHelloService3;

public class HelloService3 implements IHelloService3 {

	public boolean sayAfterThrow(String param) {
// TODO Auto-generated method stub
System.out.println("=========say after throw:" + param);
throw new RuntimeException();
} }

  配置:

<!-- 开启对Aspectj的支持 -->
<aop:aspectj-autoproxy />
<bean id="helloService" class="chapter1.service.impl.HelloService3" />
<bean id="aspect" class="chapter1.aop.HelloAspect3" />

  aop:

package chapter1.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.AfterThrowing; @Aspect
public class HelloAspect3 { @AfterThrowing(value="execution(* chapter1..*.sayAfterThrow(java.lang.String))", argNames="exception", throwing="exception")
public void afterThrowAdvice(Exception exception)
{
System.out.println("=========after throwing advice : " + exception);
}
}

  测试程序:

//后置错误通知
public void testAfterThrow()
{
ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj3.xml");
IHelloService3 hello = context.getBean("helloService", IHelloService3.class);
hello.sayAfterThrow("error");
}

  

4.环绕通知

接口:

package chapter1.server;

public interface IHelloService4 {
public void sayAround(String param);
}

  实现:

package chapter1.service.impl;

import chapter1.server.IHelloService4;

public class HelloService4 implements IHelloService4 {

	public void sayAround(String param) {
// TODO Auto-generated method stub
System.out.println("============= say around: " + param);
} }

  配置:

<!-- 开启对Aspectj的支持 -->
<aop:aspectj-autoproxy/>
<bean id="helloService" class="chapter1.service.impl.HelloService4" />
<bean id="aspect" class="chapter1.aop.HelloAspect4"/>

  aop:

package chapter1.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around; @Aspect
public class HelloAspect4 { @Around(value="execution(* chapter1..*.sayAround(java.lang.String))", argNames="param")
public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable
{
System.out.println("========= around before advice");
Object retVal = pjp.proceed(new Object[] {"我被替换了呀"});
System.out.println("========= around before advice");
return retVal; }
}

  测试程序:

//环绕通知
public void testAround()
{
ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj4.xml");
IHelloService4 hello = context.getBean("helloService", IHelloService4.class);
hello.sayAround("gaga ya xia ba");
}

  

5.引入(结合chatper1.service.IHelloService程序来试验)

接口:

package chapter1.server;

public interface IHelloService5 {
public void sayDeclare();
}

  实现:

package chapter1.service.impl;

import chapter1.server.IHelloService5;

public class HelloService5 implements IHelloService5 {

	public void sayDeclare() {
// TODO Auto-generated method stub
System.out.println("=========== say declare " );
} }

  配置:

<!-- 开启对Aspect的支持 -->
<aop:aspectj-autoproxy/>
<bean id="helloService" class="chapter1.service.impl.HelloService"/>
<bean id="aspect" class="chapter1.aop.HelloAspect5"/>

  aop:

package chapter1.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclareParents; import chapter1.server.IHelloService5;
import chapter1.service.impl.HelloService5; @Aspect
public class HelloAspect5 { @DeclareParents(
value="chapter1..*.HelloService+",
defaultImpl=HelloService5.class)
public IHelloService5 ihelloService5; }

  测试程序:

//引入
@Test
public void testDeclare()
{
ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj5.xml");
IHelloService5 hello = context.getBean("helloService", IHelloService5.class);
hello.sayDeclare();
}

  

spring3: 切面及通知实例 Aspectj的aop的更多相关文章

  1. Spring AOP前置通知实例讲解与AOP详细解析

    一.引出问题 有个接口TestServiceInter,有两个实现方法TestService和Test2Service.他们都有sayHello():我们的需求是在调用这两个方法之前,要先完成写日志的 ...

  2. Spring使用AspectJ开发AOP:基于XML

    基于XML的声明式 基于 XML 的声明式是指通过 Spring 配置文件的方式定义切面.切入点及声明通知,而所有的切面和通知都必须定义在 <aop:config> 元素中. 下面通过案例 ...

  3. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring使用AspectJ开发AOP基于XML和基于Annotation

    AspectJ 是一个基于 Java 语言的 AOP 框架,它扩展了 Java 语言.Spring 2.0 以后,新增了对 AspectJ 方式的支持,新版本的 Spring 框架,建议使用 Aspe ...

  4. (转)实例简述Spring AOP之间对AspectJ语法的支持(转)

    Spring的AOP可以通过对@AspectJ注解的支持和在XML中配置来实现,本文通过实例简述如何在Spring中使用AspectJ.一:使用AspectJ注解:1,启用对AspectJ的支持:通过 ...

  5. spring3: schema的aop与Aspectj的aop的区别

    schema的aop如下: 接口: package chapter6.service; public interface IHelloAroundService { public void sayAr ...

  6. Spring基于AspectJ的AOP的开发——注解

    源码:https://gitee.com/kszsa/dchart 一, AspectJ的概述: AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法所以它有一个专 ...

  7. Spring使用AspectJ开发AOP:基于Annotation

    基于 Annotation 的声明式 在 Spring 中,尽管使用 XML 配置文件可以实现 AOP 开发,但是如果所有的相关的配置都集中在配置文件中,势必会导致 XML 配置文件过于臃肿,从而给维 ...

  8. AspectJ对AOP的实现

    一:你应该明白的知识 1.对于AOP这种编程思想,很多框架都进行了实现.Spring就是其中之一,可以完成面向切面编程.然而,AspectJ也实现了AOP的功能,且实现方式更为简捷,使用更加方便,而且 ...

  9. Spring框架(6)---AspectJ实现AOP

    AspectJ实现AOP 上一篇文章Spring框架(4)---AOP讲解铺垫,讲了一些基础AOP理解性的东西,那么这篇文章真正开始讲解AOP 通过AspectJ实现AOP要比普通的实现Aop要方便的 ...

随机推荐

  1. Thinkphp的list_to_tree 实现无限级分类列出全部节点

    list_to_tree 使用起来十分方便,具体可查看手冊.由于我在用的时候须要同一时候列出全部节点,所以写了一个递归函数,拿出来供大家參考. public function index(){ Loa ...

  2. Python基础教程-条件判断和循环

    Python条件判断 在Python中用if语句实现: age = 20 if age >= 18: print 'your age is :',age print 'adult' 根据Pyth ...

  3. servlet中获取各种相对地址(服务器、服务器所在本地磁盘、src等)。

    [本文简介] 本文将提供javaWeb中经常使用到的相对路径的获取方法,分别有: url基本地址 带目录的url地址 服务器的根路径 服务器所在的 本地磁盘路径 服务器所在的本地磁盘路径,带文件夹 S ...

  4. C# OpenFileDialog 的使用方法

    OpenFileDialog openFileDialog = new OpenFileDialog(); //打开的文件选择对话框上的标题 openFileDialog.Title = " ...

  5. 002-es6字符串扩展

    1.字符串扩展 参考地址:http://es6.ruanyifeng.com/#docs/string 1.1.codePointAt() JavaScript 内部,字符以 UTF-16 的格式储存 ...

  6. MySQL数据库Date型数据插入问题

    MySQL数据库中,Date型数据插入问题,总是提示如下错误: “java.util.Date cannot be cast to java.sql.Date” 解决办法: 1.首先,获取Date型数 ...

  7. 基于WinIO 3.0实现驱动级键盘模拟输入

    基于WinIO 3.0实现驱动级键盘模拟输入 一个业务场景需要使用驱动级的键盘模拟,折腾了2天,总结一下,为后人节省时间. 限制条件: 1.需要真实PC机,虚拟机不行 2.仅支持PS/2 键盘(指外接 ...

  8. 使用githubs托管代码

    此文章已经发表于本人博客. 最近在学习nodejs,使用它自己都蛮觉得有激情哦,相信自己路学下去.在学习的过程中nodejs很多插件都在github上,于是自己也用了这个东东感觉不错,开始的时候还用命 ...

  9. HDU - 4407 Sum (容斥)

    题意:初始序列[1..N](1<=N<=4e5),支持两种操作:1.求区间[x,y]内与p互素的数之和: 2.将x位置的数变为c. 分析:很容易把人骗到线段树的思维中,而实际上操作2单点的 ...

  10. [转]让你从零开始学会写爬虫的5个教程(Python)

    让你从零开始学会写爬虫的5个教程(Python)   写爬虫总是非常吸引IT学习者,毕竟光听起来就很酷炫极客,我也知道很多人学完基础知识之后,第一个项目开发就是自己写一个爬虫玩玩. 其实懂了之后,写个 ...