xml配置实现

先写三个类

public String amethod(String s) {
  System.out.println("This is AAAAAAAAAAAAAAAA");
  return "This is A return :"+s;
 }
public class B {
public void bmethod() {
System.out.println("This is BBBBBBBBBBBBBBBBBBBBB ");
}
}
//这个类实现了spring里的接口,在配置文件中配置advisor的bean
public class C implements AfterReturningAdvice{

 @Override
 public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {

  System.out.println("This is returnValue:"+returnValue.toString());
  System.out.println("This is method:"+method.getName());
  for (Object arg : args) {
   System.out.println("This is args:"+arg.toString());
  }
  System.out.println("This is target:"+target);
 }
}
 

spring配置文件内容

<bean id="a" class="com.hehe.aop.A" />
 <bean id="b" class="com.hehe.aop.B" />
 <bean id="c" class="com.hehe.aop.C" />
 <aop:config>
  <aop:pointcut id="p" expression="execution(* com.hehe.aop.A.*(..))" />
  <!-- advicor 要实现spring里的接口 -->
  <aop:advisor advice-ref="c" pointcut-ref="p" />
  <!-- 切面,普通类,b的bmethod方法切入定义好的切点位置 -->
  <aop:aspect ref="b">
   <!-- 切点可以写表达式,也可以引用定义好的,下面两种效果一样 -->
   <!-- <aop:before method="bmethod" pointcut="execution(* com.hehe.aop.A.*(..))" /> -->
   <aop:before method="bmethod" pointcut-ref="p" />
  </aop:aspect>
 </aop:config>

写个执行方法

public static void main(String[] args) {
  ApplicationContext context = new ClassPathXmlApplicationContext("spring-aop.xml");
  A a =  (A) context.getBean("a");
  a.amethod("Hello world");
 }

执行结果:

This is BBBBBBBBBBBBBBBBBBBBB        //切点类之前执行
This is AAAAAAAAAAAAAAAA                     //切点类
This is returnValue:This is A return :Hello world //下面语句是切点类之后执行结果
This is method:amethod
This is args:Hello world
This is target:com.hehe.aop.A@7fa98a66

注解实现

先写俩类

public class A {
public String amethod(String s) {
System.out.println("This is AAAAAAAAAAAAAAAA");
return "This is A return :"+s;
}
} @Component
@Aspect
public class D { @AfterReturning(value = "execution(* com.hehe.aop.A.*(..))",returning="returnValue")
public void dmethod(JoinPoint j,Object returnValue) {
System.out.println(returnValue.toString());
String name = j.getSignature().getName();
System.out.println(name);
}
} //好几个joinpoint,用这个:import org.aspectj.lang.JoinPoint;

配置文件

<context:component-scan base-package="com.hehe.aop" />
<aop:aspectj-autoproxy/>//这个必须要有,没有还不报错。
<bean id="a" class="com.hehe.aop.A" />

测试类

public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-aop.xml");
A a = (A) context.getBean("a");
a.amethod("Hello world");
}
}

测试结果

This is AAAAAAAAAAAAAAAA
This is A return :Hello world
amethod

spring切面编程的更多相关文章

  1. spring切面编程AOP 范例一

    参照网上的spring AOP编程实例进行配置,但是碰到了几个坑.这篇文章重点讲解一下我踩过的两个坑: 1.使用@Service自动装配的时候,基础扫描包配置要正确: 2.xml中切面配置中的exec ...

  2. Spring切面编程步骤

    什么是面向切面编程 面向对象的编程主要注重核心业务,而面向切面编程主要关注一些不是核心的业务,但又是必须的辅助功能,比如一个完整的系统中,记录平时系统运行时抛出的异常,需要我们去记录,以便我们对系统尽 ...

  3. Spring切面编程实践【原创】

    定义 什么叫Spring面向切面编程(AOP),请自行百度,这边就不做详细介绍了. 场景 有两个对象,字典和工程信息Bean,每次新增或修改对象时,记录新增和修改的时间. 基类定义 package m ...

  4. spring aop使用,spring aop注解,Spring切面编程

    ================================ ©Copyright 蕃薯耀 2020-01-21 https://www.cnblogs.com/fanshuyao/ 一.第一步, ...

  5. Spring切面编程AOP

  6. Spring切面编程Aspect之@Before和@Around用法

    查看dao层使用的sql import java.util.Arrays; import org.apache.commons.lang.ArrayUtils; import org.aspectj. ...

  7. spring aop 切面编程中获取具体方法的方法

    spring 切面编程中获取具体方法的方法 工作中,使用环绕通知,用来捕获异常,然后通过获取方法的返回值,返回不同的数据给到调用方. 由于方法的返回值不同,我们处理异常时,也需要返回不同的格式. 这时 ...

  8. spring入门(四)【面向切面编程】

    开发过程中很多时候会用到日志.事务等操作,这些操作如果要写在业务代码中会相当麻烦,这时就会用到面向切面编程(AOP),AOP作为一种编程思想,和OOP有着不同的侧重点,面向对象侧重于万事万物皆对象,而 ...

  9. Spring面向切面编程(AOP)

    1 spring容器中bean特性 Spring容器的javabean对象默认是单例的. 通过在xml文件中,配置可以使用某些对象为多列. Spring容器中的javabean对象默认是立即加载(立即 ...

随机推荐

  1. 树莓派4B踩坑指南 - (12)谷歌浏览器书签同步

    书签和插件不能同步真的是不方便..使用时删掉※符号 过程比较复杂,坑很多,但确认有效 免费访问说明: https://github.com/max2max/fre※es※s 软件安装 https:// ...

  2. Python基础语法笔记2

    ------------------------------------------------------------------------------- 常量和Pylint的规范 1.常量:常量 ...

  3. 【Python矩阵及其基础操作】【numpy matrix】

    一.矩阵生成 1.numpy.matrix: import numpy as np x = np.matrix([ [1, 2, 3],[4, 5, 6] ]) y = np.matrix( [1, ...

  4. jquery-ajax的用法

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. Chrome浏览器 无需安装插件将整个网页另存为图片

    步骤1 ctrl+shift+i 步骤2 ctrl+shift+p 步骤3 输入full 回车确定 感谢先辈们的无私奉献!原文https://blog.csdn.net/wumingid/articl ...

  6. Python 爬取 热词并进行分类数据分析-[JSP演示+页面跳转]

    日期:2020.02.03 博客期:142 星期一 [本博客的代码如若要使用,请在下方评论区留言,之后再用(就是跟我说一声)] 所有相关跳转: a.[简单准备] b.[云图制作+数据导入] c.[拓扑 ...

  7. 2019年ipa发布苹果应用商店审核指南

    https://baijiahao.baidu.com/s?id=1623886553597961077&wfr=spider&for=pc ipa 发布审核指南 说明: 本指南为初版 ...

  8. PageObject

    import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetExplorerDriver; import o ...

  9. JAVA高级编程数据源datasource

    原文链接 数据源 通过jdbc连接数据库,多建立几条连接放在数据源里面.可以设置数据源的最大连接数,同时活跃的连接数,最少空闲的连接数,能够同时接收处理的连接数等等. dbcp数据源 需要的jar包: ...

  10. 吴裕雄--天生自然ORACLE数据库学习笔记:SQL语言基础

    select empno,ename,sal from scott.emp; SELECT empno,ename,sal FROM scott.emp; selECT empno,ename,sal ...