spring切面编程
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 void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
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="b" class="com.hehe.aop.B" />
<aop:config>
<aop:pointcut id="p" expression="execution(* com.hehe.aop.A.*(..))" />
<aop:advisor advice-ref="c" pointcut-ref="p" />
<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切面编程的更多相关文章
- spring切面编程AOP 范例一
参照网上的spring AOP编程实例进行配置,但是碰到了几个坑.这篇文章重点讲解一下我踩过的两个坑: 1.使用@Service自动装配的时候,基础扫描包配置要正确: 2.xml中切面配置中的exec ...
- Spring切面编程步骤
什么是面向切面编程 面向对象的编程主要注重核心业务,而面向切面编程主要关注一些不是核心的业务,但又是必须的辅助功能,比如一个完整的系统中,记录平时系统运行时抛出的异常,需要我们去记录,以便我们对系统尽 ...
- Spring切面编程实践【原创】
定义 什么叫Spring面向切面编程(AOP),请自行百度,这边就不做详细介绍了. 场景 有两个对象,字典和工程信息Bean,每次新增或修改对象时,记录新增和修改的时间. 基类定义 package m ...
- spring aop使用,spring aop注解,Spring切面编程
================================ ©Copyright 蕃薯耀 2020-01-21 https://www.cnblogs.com/fanshuyao/ 一.第一步, ...
- Spring切面编程AOP
- Spring切面编程Aspect之@Before和@Around用法
查看dao层使用的sql import java.util.Arrays; import org.apache.commons.lang.ArrayUtils; import org.aspectj. ...
- spring aop 切面编程中获取具体方法的方法
spring 切面编程中获取具体方法的方法 工作中,使用环绕通知,用来捕获异常,然后通过获取方法的返回值,返回不同的数据给到调用方. 由于方法的返回值不同,我们处理异常时,也需要返回不同的格式. 这时 ...
- spring入门(四)【面向切面编程】
开发过程中很多时候会用到日志.事务等操作,这些操作如果要写在业务代码中会相当麻烦,这时就会用到面向切面编程(AOP),AOP作为一种编程思想,和OOP有着不同的侧重点,面向对象侧重于万事万物皆对象,而 ...
- Spring面向切面编程(AOP)
1 spring容器中bean特性 Spring容器的javabean对象默认是单例的. 通过在xml文件中,配置可以使用某些对象为多列. Spring容器中的javabean对象默认是立即加载(立即 ...
随机推荐
- 关于找不到指定的模块,异常来自HRESULT:0x8007007E的解决方法
上午从公司前辈那里拷贝到的ASP.NET代码,在自己机器上部署的时候发现问题,直接报错,找不到指定的模块,异常来自HRESULT:0x8007007E.并且一大堆警告. 在网上百度很多解决方法,归纳如 ...
- 基于通用二进制方式安装MySQL-5.7.24(比源码安装MySQL快许多)及破密码
确保系统中有依赖的libaio软件 yum -y install libaio 使用wget命令下载mysql-5.7.24软件包 wget http://mirrors.sohu.com/mysql ...
- 【网摘】模仿 placeholder 属性
/*为空时显示 element attribute content*/ .project-task-edit .subtask-body-txt:empty:before { content: att ...
- Python 基础之面向对象之类中的方法和与类相关的魔术属性以及反射
一.类中的方法 普通方法: 没有参数,只能类调用绑定方法: (1)绑定到对象(自动传递对象参数) (2)绑定到类(自动传递参数)静态方法: 无论是类还是对象,都能调用的方法#例:class Dog() ...
- oracle的decode、sign、nvl,case...then函数
ORACLE几种常用的方法 1.decode 常见的用法 : 格式:decode(condition,value1,result[, value2,result2], default_result) ...
- Python压缩文件/文件夹
[Python压缩文件夹]导入“zipfile”模块 def zip_ya(startdir,file_news): startdir = ".\\123" #要压缩的文件夹路径 ...
- Java--API解读之Method Summary
参考来源:Java 中静态方法 实例方法 具体方法区别与联系 JAVA Method Summary网页 * Static Method :"静态方法",直接引用,无需创建对象: ...
- LeetCode简单题(一)
题目一: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组 ...
- ApacheDbUtilsUpdate
ApacheDbUtilsUpdate package p1; import com.DataSourceUtil; import org.apache.commons.dbutils.QueryRu ...
- C++11类内static成员变量声明与定义
众所周知,将一个类内的某个成员变量声明为static型,可以使得该类实例化得到的对象实现对象间数据共享. 在C++中,通常将一个类的声明写在头文件中,将这个类的具体定义(实现)写在cpp源文件中. 因 ...