AspectJ-Based方式的AOP,通知类不需要实现任何接口,且前置通知,后置通知,环绕通知,异常通知都可以写在一个类中,下面先实现一个简单的,不带参数的通知。

第一步定义通知类,通知类中的通知(方法)名称可以任意取

package com.airplan.pojo;

import org.aspectj.lang.ProceedingJoinPoint;

public class AspectJAdvice {
/*
* 前置通知
*/
public void before() {
System.out.println("前置通知执行");
} /*
* 后置通知,after通知,无论切点是否发生异常都会执行
*/
public Object after() {
System.out.println("after 通知执行了");
return null;
} /*
* 后置通知,after-returning,只有切点正常结束时才会执行
*/
public Object afterReturn() {
System.out.println("afterReturn 通知执行了");
return null;
} /**
* 环绕通知需要有一个返回值,以及一个参数
* @param p
* @return
* @throws Throwable
*/
public Object arround(ProceedingJoinPoint p) throws Throwable{
System.out.println("执行环绕");
System.out.println("环绕-前置");
Object result = p.proceed();
System.out.println("环绕后置");
return result;
} /*
* 异常通知
*/
public void mythrow(){
System.out.println("异常");
}
}

第二步配置

<?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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 第一步:注册通知类 ,与切点类--> <bean id="advices" class="com.airplan.pojo.AspectJAdvice"></bean>
<bean id="pointCutClass" class="com.airplan.pojo.PointCutClass"></bean>
<!-- 第二步:配置切面 -->
<aop:config>
<!-- 配置切面,绑定通知类 -->
<aop:aspect ref="advices">
<!-- 配置切点 -->
<aop:pointcut expression="execution(* com.airplan.pojo.PointCutClass.*(..))" id="pointCutFun"/>
<!-- 配置通知与切点关联 -->
<aop:after method="after" pointcut-ref="pointCutFun"/>
<aop:before method="before" pointcut-ref="pointCutFun"/>
<aop:after-returning method="afterReturn" pointcut-ref="pointCutFun"/>
<aop:around method="arround" pointcut-ref="pointCutFun"/>
<aop:after-throwing method="mythrow" pointcut-ref="pointCutFun"/>
</aop:aspect>
</aop:config> </beans>

第三步:测试代码

package com.airplan.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.airplan.pojo.PointCutClass; public class AopDemo {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
PointCutClass pc = ac.getBean("pointCutClass",PointCutClass.class);
pc.pointCutFun();
}
}

spring学习 十一 AspectJ-based的通知入门 不带参数的通知的更多相关文章

  1. iOS NSNotification传递带参数的通知

    普通的通知使用 注册观察者 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getNotificat ...

  2. Spring学习--用 ASpectJ 注解实现 AOP

    用 AspectJ 注解声明切面: 要在 Spring 中声明 AspectJ 切面 , 只需要在 IOC 容器中将切面声明为 bean 实例.当在 Spring IOC 容器中初始化 AsjectJ ...

  3. Spring学习(十一)-----Spring使用@Required注解依赖检查

    Spring学习(九)-----Spring依赖检查 bean 配置文件用于确定的特定类型(基本,集合或对象)的所有属性被设置.在大多数情况下,你只需要确保特定属性已经设置但不是所有属性.. 对于这种 ...

  4. spring框架应用系列三:切面编程(带参数)

    本文系作者原创,转载请注明出处:http://www.cnblogs.com/further-further-further/p/7786715.html 解决问题 1.分离业务监控与业务处理.简单点 ...

  5. spring学习 十二 AspectJ-based的通知入门 带参数的通知

    第一步:编写通知类 package com.airplan.pojo; import org.aspectj.lang.ProceedingJoinPoint; public class Advice ...

  6. Spring学习_day02_AOP,AspectJ,JdbcTemplate

    本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! Spring_day02 一.AOP面向切面编程 1. ...

  7. Spring学习之Aspectj开发实现AOP

    Aspectj是一个基于Java语言的Aop框架,它提供了强大的Aop功能. Aspectj简介: 1.Aspectj是一个面向切面的框架,它扩展了Java语言,它定义了一个Aop语法. 2.所以它有 ...

  8. Spring学习之路-从放弃到入门

    AOP:方法拦截器 IOC:类管理容器 主要讲讲这一天看Spring视频学到的东西,以下的叫法全是自创的. 1.类实例管理容器 关于Spring,首先是对类的管理,在常规情况,生成一个类需要调用new ...

  9. Spring学习十一

    一:  创建bean的方法: 1: 如果不采用构造注入:默认调用bean的无参构造函数,因此该类必须要提供无参构造函数,用无参构造函数用反射创建bean. :               如果采用构造 ...

随机推荐

  1. 【OpenGL】glsl、glew、glfw

    glsl: OpenGL着色语言(OpenGL Shading Language)是用来在OpenGL中着色编程的语言,也即开发人员写的短小的自定义程序,他们是在图形卡的GPU (Graphic Pr ...

  2. SSM的例子-参考

    ssm的例子:http://blog.csdn.net/double030/article/details/63683613

  3. div 绑定keyup

    参考 https://www.cnblogs.com/Struts-pring/p/4290740.html <div tabindex="-1" onkeyup=" ...

  4. 八 xml模块

    xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统的 ...

  5. as3.0影片简介失效,不阻碍下面影片简介的事件

    mast.mouseEnabled=false; mast.mouseChildren=false;

  6. WLC5520无法通过无线客户端进行网管故障解决

    客户反馈其办公环境中的WLC5520网管需要通过内部有线网络进行管理,通过无线客户端无法进行管理,远程协助其开启WLC5520的无线管理功能后故障解决.

  7. JavaScript各种继承方式(一):原型链继承(prototype chaining)

    一 原理 子类的构造函数的原型对象,是父类的构造函数创建的实例. function Fruit(){ this.name = '水果'; this.nutrition=['维生素','膳食纤维']; ...

  8. RxJS之catchError

    Catches errors on the observable to be handled by returning a new observable or throwing an error. 返 ...

  9. matlab基础绘图知识

    axis([xmin xmax ymin ymax])   %设置坐标轴的最小最大值 xlabel('string')                             %标记横坐标 ylabe ...

  10. PTA 7-8 哈利·波特的考试(floyd)

    哈利·波特要考试了,他需要你的帮助.这门课学的是用魔咒将一种动物变成另一种动物的本事.例如将猫变成老鼠的魔咒是haha,将老鼠变成鱼的魔咒是hehe等等.反方向变化的魔咒就是简单地将原来的魔咒倒过来念 ...