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. MySQL 远程连接问题 (Linux Server)

    Mysql Workbench 连接Ubuntu上的Mysql时报如下错误: 原因:查看  /etc/mysql/mysql.conf.d/mysqld.cnf # # Instead of skip ...

  2. ios中为视图添加圆角

    1.使用 layer设置指定圆角 或者设定一个或几个圆角 码修 关注 2017.04.20 19:03* 字数 107 阅读 656评论 0喜欢 0 由于项目中需要给按钮左下 和左上加圆角,我司可爱的 ...

  3. selenium+chrome options

    selenium+chrome options 环境:selenium chrome 1.      selenium + chrome参数配置 1.1.    启动 from selenium im ...

  4. SOAP1.1 VS SOAP1.2

    SOAP提升: 目前WebService的协议主要有SOAP1.1和1.2.两者的命名空间不同. 见下页对比 SOAP1.1版本与SOAP1.2版本在头信息上存在差异.SOAP1.1存在SOAPAct ...

  5. 并发编程之Event事件

    Event事件 用来同步线程之间的状态. 举个例子: ​ 你把一个任务丢到了子线程中,这个任务将异步执行.如何获取到这个任务的执行状态 解决方法: 如果是拿到执行结果 我们可以采用异步回调, 在这里我 ...

  6. day1-2基本数据类型

    /*5种基本数据类型 1,Undefined var a; a = undefined Undefined派生自Null 2,Null(本质属于object,单独细分出来的) var a = null ...

  7. java 实现用户自由选择字段实现导出EXCEL表格

    package com.thinkgem.jeesite.common.utils.excel; import java.io.File; import java.io.OutputStream; i ...

  8. JS原型与原型链继承的理解

    一.原型 先从构造函数开始吧! 构造函数是什么?构造函数与其他函数唯一的区别在于调用方式不同.任何函数只要通过new来调用就可以作为构造函数,它是用来创建特定类型的对象. 下面定义一个构造函数 Fem ...

  9. LCS(最长公共子序列)

    这个问题很有意思,在生物应用中,经常需要比较两个(或多个)不同生物体的DNA片段.例如,某种生物的DNA可能为S1 = ACCGGTCGAGTGCGCGGAAGCCGGCCGAA,S2 = GTCGT ...

  10. Spring 各个组件架构