spring aop自动代理注解配置之一
<?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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <context:component-scan base-package="com.zr.utils"></context:component-scan> <!-- <aop:aspectj-autoproxy ></aop:aspectj-autoproxy> -->
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
</beans>
package com.zr.utils;
import org.springframework.stereotype.Component;
@Component("comp")
public class Calculate implements Compute{
public int div(int num1,int num2){
System.out.println("除法");
return num1/num2;
}
}
package com.zr.utils; import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; @Aspect
@Component("testAspect")
public class TestAspect { //第一个 * 代表任意修饰符及任意返回值,其中 .. 匹配任意数量的参数.
@Before("execution(* com.zr.utils.Calculate.div(..))")
public void methodBefore(){
System.out.println("执行方法之前");
}
//第一个 * 代表public修饰符下任意返回值,第一个 * 代表com.zr.utils.Calculate路径下的任意方法
@After("execution(public * com.zr.utils.Calculate.*(..))")
public void methodAfter() {
System.out.println("执行方法之后");
} //匹配第一个参数为 int 类型的方法, .. 匹配任意数量任意类型的参数
@AfterReturning("execution(public int com.zr.utils.Calculate.*(int,..))")
public void methodAfterRunning(){
System.out.println("返回结果之后执行");
}
//匹配参数类型为 int, int 类型的方法.
@AfterThrowing("execution(public int com.zr.utils.Calculate.*(int,int))")
public void methodAfterThrowing(){
System.out.println("异常通知, 在方法抛出异常之后执行");
}
}
package com.zr.utils; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestAop { public static void main(String[] args) { ApplicationContext ctc = new ClassPathXmlApplicationContext("context.xml"); /*Calculate calculate = (Calculate) ctc.getBean("comp");*/
Calculate c = (Calculate) ctc.getBean("comp");
int result = c.div(10, 2); }
}
spring aop自动代理注解配置之一的更多相关文章
- spring aop自动代理注解配置之二
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- spring aop自动代理xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(一)入口
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...
- Spring AOP 动态代理 缓存
Spring AOP应用:xml配置及注解实现. 动态代理:jdk.cglib.javassist 缓存应用:高速缓存提供程序ehcache,页面缓存,session缓存 项目地址:https://g ...
- 使用BeanNameAutoProxyCreator实现spring的自动代理
提到代理,我们可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames实现,但如果需要代理的bean很多,无疑会对spring配置 ...
- Hibernate 延迟加载的代理模式 和 Spring AOP的代理模式
Hibernate 延迟加载的代理模式 和 Spring AOP的代理模式 主题 概念 Hibernate 延迟加载的代理模式 Spring AOP的代理模式 区别和联系 静态代理和动态代理 概念 代 ...
- 运用Spring Aop,一个注解实现日志记录
运用Spring Aop,一个注解实现日志记录 1. 介绍 我们都知道Spring框架的两大特性分别是 IOC (控制反转)和 AOP (面向切面),这个是每一个Spring学习视频里面一开始都会提到 ...
随机推荐
- Spring字符编码过滤器
1 <filter> 2 <filter-name>characterEncodingFilter</filter-name> 3 <filter-class ...
- 【如何入门ACM】
第一阶段:先刷水题,水题,就是几乎不牵扯算法.需要自己想方法解决.这样的题,一是锻炼逻辑和思维的严谨,二是锻炼代码能力.一般做到60-200题左右. 第二阶段:渐渐的学一些简单的算法,或者专题训练,或 ...
- 《DSP using MATLAB》示例Example7.15
代码: %T1 = 0.5 M = 40; alpha = (M-1)/2; l = 0:M-1; wl = (2*pi/M)*l; Hrs = [ones(1, 5), 0.5, zeros(1, ...
- 剑指offer-第五章优化时间和空间效率(连续子数组的最大和)
题目:输入一个数组,数组中有正也有负,数组中连续的一个或者连续的多个数字组成一个子数组.求所有的子数组和的最大值.要求时间复杂度为O(n) 思路:我们的最直观的想法就是求出这个数组中的所有的子数组,然 ...
- Hadoop体系结构之 HDFS
HDFS采用主从(Master/Slave)结构模型,一个HDFS集群是由一个NameNode和若干个DataNode组成的(在最新的Hadoop2.2版本已经实现多个NameNode的配置-这也是一 ...
- [转]Script标签和脚本执行顺序
Script标签和脚本执行顺序 这里详细聊聊和script标签相关的脚本执行顺序. Script标签的默认行为 几个首要特性: script标签(不带defer或async属性)的会阻止文档渲染.相关 ...
- React组件性能优化总结
性能优化的思路 影响网页性能最大的因素是浏览器的重排(repaint)和重绘(reflow). React的Virtual DOM就是尽可能地减少浏览器的重排和重绘. 从React渲染过程来看,如何防 ...
- 使用spring框架的JdbcTemplate实现对Oracle数据库的简单操作实例
最近实现了一个小功能,针对Oracle数据库两张关联表进行查询和修改,因为比较简单,所以选择了spring框架里的JdbcTemplate.JdbcTemplate算是老古董了,是当年spring为了 ...
- DLatch by Verilog
//-----------------------------------------------------// Design Name : dlatch_reset// File Name : ...
- 安装nagios-plugins插件make时遇到的error
安装nagios-plugins插件make时遇到的error error内容: check_http.c: In function ‘process_arguments’: check_http.c ...