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. 通过DataTable获得表的主键

    转载http://www.cnblogs.com/hobe/archive/2005/10/07/249940.html 通过DataTable获得表的主键 很多情形下我们需要知道表的主键是什么.在A ...

  2. 批量执行命令(SSH)

    for a in {1..6} ; do scp /etc/hosts enc-bigdata0$a:/etc/hosts ; done

  3. 第十章 优先级队列 (b1)完全二叉堆:结构

  4. pandas_1

    大熊猫10分钟 这是对熊猫的简短介绍,主要面向新用户.您可以在Cookbook中看到更复杂的食谱. 通常,我们导入如下: In [1]: import numpy as np In [2]: impo ...

  5. 2019-3-10——生成对抗网络GAN---生成mnist手写数字图像

    """ 生成对抗网络(GAN,Generative Adversarial Networks)的基本原理很简单: 假设有两个网络,生成网络G和判别网络D.生成网络G接受一 ...

  6. Meta-Analysis

    meta-analysis是用统计的概念与方法,去收集.整理与分析之前学者专家针对某个主题所做的众多实证研究,希望能够找出该问题或所关切的变量之间的明确关系模式,可弥补传统的Review Articl ...

  7. Mybatis 表名是变量时

    写语句时这样就可以了 <select id="selectTotal" resultType="java.util.Map" parameterType= ...

  8. 8.16 val()和html()的问题

    今天在做关闭模态框重置表单时,关闭模态框后输入框里的值还是在,不知道怎么回事? 感谢wd啦,原来我在初始化这个输入框的时候就写错了,输入框写值的时候用的是val(),而我和上面的div一样,用的是ht ...

  9. 安装sql server 2008 提示错误 SQL Server 2005 Express 工具。 失败

    安装sql server 2008 management,提示错误:Sql2005SsmsExpressFacet 检查是否安装了 SQL Server 2005 Express 工具. 失败,已安装 ...

  10. SAP transportation

    1.CONFIGURATION TRANSPORT flow:DEV(100) --scc1--> DEV(400) --STMS after release-> QAS(510) --S ...