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. cross-origin HTTP request

    w https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS A resource makes a cross-ori ...

  2. (转)VLC播放RTP打包发送的.264文件

    VLC播放RTP打包发送的.264文件 1,要有一个发送RTP包的264文件的服务器; 具体代码如下: rtp.h #include <WinSock2.h> #pragma commen ...

  3. 记一次centos7挂在nas盘的踩坑经过

    p:first-child, #write > ul:first-child, #write > ol:first-child, #write > pre:first-child, ...

  4. 用Maven构建Mahout项目实现协同过滤userCF--单机版

    本文来自:http://blog.fens.me/hadoop-mahout-maven-eclipse/ 前言 基于Hadoop的项目,不管是MapReduce开发,还是Mahout的开发都是在一个 ...

  5. 吴超老师课程--Hbase介绍和伪分布式安装

    1.HBase(NoSQL)的数据模型1.1 表(table),是存储管理数据的.1.2 行键(row key),类似于MySQL中的主键.     行键是HBase表天然自带的.1.3 列族(col ...

  6. MAC下配置MAVEN环境变量配置

    MAVEN环境变量的配置: 第一步:在MAVEN的官网下载MAVEN.http://maven.apache.org/download.cgi,我这里下载的是apache-maven-3.39-bin ...

  7. PAT 天梯赛 L1-010. 比较大小 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-010 AC代码 #include <iostream> #include <cstdio&g ...

  8. PAT 天梯赛 L1-001 【水】

    L1-001. Hello World 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 这道超级简单的题目没有任何输入. 你只需要在一行中输 ...

  9. java byte为何范围是-128~127

    从我们接触Java的时候,就被告知基础类型byte是一个字节,占8位,表示的范围是-128~127.那么为什么会这个范围呢?   咱们先回顾一下计算机基础: 1. 在计算机内部数据的存储和运算都采用二 ...

  10. [转]HBase hbck——检察HBase集群的一致性

    Hbase提供了hbck命令来检查各种不一致问题.hbck的名字仿效了HDFS的fsck命令,后者是一个用于检查HDFS中不一致问题的工具.下面这段非常易懂的介绍出自于hbck的源程序. 检查数据在M ...