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对象默认是立即加载(立即 ...
随机推荐
- vue 组件,以及组件的复用
有时候代码的某一模块可能会经常使用到,那么完全可以把这一模块抽取出来,封装为一个组件,哪里需要用到的时候只需把模块调用即可 .参考vue官方 https://cn.vuejs.org/v2/guide ...
- Solidity基本数据结构
任何一个智能合约都会在最开头表示使用的编译器版本 如:prama solidity ^0.4.0 数组: //静态数组 大小长度确定 uint[2] fixedArray; //动态数组,可以随意添加 ...
- idea设置代码提示忽略大小写
- 新手如何配置 Chromedriver 环境变量
有一个不错的链接:https://blog.csdn.net/qq_41429288/article/details/80472064
- 并发编程之GIL
目录 GIL 什么是GIL锁 为什么需要加锁 带来的问题 如何解决 关于性能的讨论 计算密集型任务:进程执行更快 IO密集型:线程执行更快 自定义锁与GIL的区别 GIL 什么是GIL锁 官方解释: ...
- tomcat中servlet冲突问题
在启动tomcat以后,控制台发现“Offending class: javax/servlet/Servlet.class”信息: 信息: validateJarFile(E:\code\MyApp ...
- linux#自启动脚本
编写脚本: /etc/init.d/myscriptname # chkconfig: # description: 描述信息,描述信息,上面的90表示在众多开机启动脚本的优先级,10表示在众多关机启 ...
- canvas画扇形、饼图
画扇形的方法 方法一:起始角度是0,那么第一条线就是line(r,0),通过旋转扇形的角度,第二条线就是line(r,0) //圆弧 ctx.save(); ctx.translate(100, 10 ...
- Codeforces Round #620 (Div. 2) 题解
A. Two Rabbits 思路: 很明显,如果(y-x)%(a+b)==0的话ans=(y-x)/(a+b),否则就为-1 #include<iostream> #include< ...
- virtual column make sqlserver using function index
In sqlserver, it is impossible that if we want to create an function index. Doesn`t means we can not ...