Spring的aop机制提供两类方式实现类代理。一种是单个代理,一种是自动代理。

单个代理通过ProxyFactoryBean来实现(就如上面的配置)。

自动代理:自动代理能够让切面定义来决定那个bean需要代理,不需要我们为特定的bean明确的创建代理从而提供一个更完整的aop实现 通过BeanNameAutoProxyCreator或者 DefaultAdvisorAutoProxyCreator实现。

◆采用单个代理方式 (费时费力,配置复杂臃肿)

下面就采用自动代理

实现代理bean的两种方式:

1,“基于Spring上下文的里声明的通知者bean的基本自动代理”:通知者的切点表达式用于决定哪个bean和那些方法需要被代理

2,”基于@AspectJ注释驱动切面的自动代理”:切面里包含的通知里指定的切点将用于选择哪个bean和哪个方法要被代理

第一种:<!——自动代理增加此行,容器会自动根据通知要匹配的切入点,为包含切入点的类创建 代理。注意这个bean没有id,因为永远都不会直接引用它——>

feedom.net   

第二种 自动代理@AspectJ切面

然而aspectJ提供可一种基于jdk1.5注解技术的方式,使得配置文件更少,更方便。能够把POJO类注释为切面这通常称为

@AspectJ.我们利用@AspectJ注释,我们不需要声明任何额外的类或Bean就可以把POJO转换成一个切面例如:

@Aspect 定义切面不再是普通的POJO了 在POJO类中加注释


public class AspectJMixAspect { private Log logger = LogFactory.getLog(AspectJMixAspect.class); @Pointcut("execution(* *..HelloIF.*(..)) || execution(* *..TestBeanIF.*(..))")定义切入点那些类的那些方法添加 public void allMethods() { } @Pointcut("execution(* *..TestBeanIF.toDate(..)) && args(dateStr)") public void toDate(String dateStr) { } @Around("allMethods()") 环绕通知 public Object timing(ProceedingJoinPoint pjp) throws Throwable { long begin = System.currentTimeMillis(); Object ret = pjp.proceed(); long end = System.currentTimeMillis(); String methodName = pjp.getSignature().getName(); String targetClass = pjp.getTarget().getClass().getName(); logger.info("Around advice: It took " + (end - begin) + "ms to call " + methodName + " on " + targetClass); return ret; } @Before("allMethods()") public void logBefore(JoinPoint jp) { logger.info("Before advice: " + jp.getSignature().toLongString()); } @AfterReturning(value="toDate(dateStr)", returning = "date", argNames = "date, dateStr") public void afterReturning(Date date, String dateStr) { logger.info("call method toDate(" + dateStr + ") and return " + date); } @AfterThrowing(value="toDate(dateStr)", throwing="ex", argNames="dateStr, ex") public void afterThrowing(String dateStr, ParseException ex){方法名任意但参数要和argNames=""中的参数顺序一样, } }

配置文件


<?xml version="1.0" encoding="UTF-8"?> <p:beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd "> <!-- target --> <p:bean id="hello" class="com.kettas.spring.ioc.day1.HelloIFImpl"> <p:property name="cal"> <p:bean class="java.util.GregorianCalendar"></p:bean> </p:property> <p:property name="user" value="world"></p:property> </p:bean> <p:bean id="testBean" class="com.kettas.spring.aop.day5.TestBean"> <p:property name="pattern" value="yyyy-MM-dd"></p:property> </p:bean> <!-- apsect bean --> <p:bean id="aspectJAspect" class="com.kettas.spring.aop.day5.AspectJMixAspect"></p:bean> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 声明自动代理bean需要命名空间:aop="http://www.springframework.org/schema/aop" </p:beans>

AOP的自动代理的更多相关文章

  1. day39 07-Spring的AOP:自动代理

    带有切点的切面或者是不带有切点的切面配置一个类就要配置一段生成代理的代码,这样太麻烦了. 选中orderDao右键watch JDK动态代理是先创建被代理对象,然后在创建代理对象的时候传入被代理对象. ...

  2. Spring AOP使用整理:自动代理以及AOP命令空间

    三.自动代理的实现 1.使用BeanNameAutoProxyCreator 通过Bean的name属性自动生成代理Bean. <bean class="org.springframe ...

  3. spring8——AOP之Bean的自动代理生成器

    对于上篇博客http://www.cnblogs.com/cdf-opensource-007/p/6464237.html结尾处提到的两个问题,可以使用spring提供的自动代理生成器解决.自动代理 ...

  4. Spring框架学习08——自动代理方式实现AOP

    在传统的基于代理类的AOP实现中,每个代理都是通过ProxyFactoryBean织入切面代理,在实际开发中,非常多的Bean每个都配置ProxyFactoryBean开发维护量巨大.解决方案:自动创 ...

  5. Spring -- aop(面向切面编程),前置&后置&环绕&抛异常通知,引入通知,自动代理

    1.概要 aop:面向方面编程.不改变源代码,还为类增加新的功能.(代理) 切面:实现的交叉功能. 通知:切面的实际实现(通知要做什么,怎么做). 连接点:应用程序执行过程期间,可以插入切面的地点. ...

  6. 死磕Spring之AOP篇 - Spring AOP自动代理(一)入口

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  7. 死磕Spring之AOP篇 - Spring AOP自动代理(二)筛选合适的通知器

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  8. 死磕Spring之AOP篇 - Spring AOP自动代理(三)创建代理对象

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  9. 使用BeanNameAutoProxyCreator实现spring的自动代理

    提到代理,我们可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames实现,但如果需要代理的bean很多,无疑会对spring配置 ...

随机推荐

  1. bzoj 3678 wangxz与OJ

    3678: wangxz与OJ Time Limit: 10 Sec  Memory Limit: 128 MBhttp://www.lydsy.com/JudgeOnline/problem.php ...

  2. LightOJ 1017 - Brush (III) 记忆化搜索+细节

    http://www.lightoj.com/volume_showproblem.php?problem=1017 题意:给出刷子的宽和最多横扫次数,问被扫除最多的点是多少个. 思路:状态设计DP[ ...

  3. CSS 字体常用属性

    一.字体大小  font-size:参数 /** * 参数:一.数字固定值,如20px * 二.父元素字体的百分比 * 三.smaller 比父元素更小 * 四.larger 比父元素更大 * 五.i ...

  4. php输出日志的实现

    php输出日志的实现 思想:在想要输出log日志的地方,使用php的写入文件函数,把数据写入到事先定义好的文件中. php代码如下: //输出日志 public function outputLog( ...

  5. 实用技巧:如何用 CSS 做到完全垂直居中

    本文将教你一个很有用的技巧——如何使用 CSS 做到完全的垂直居中.我们都知道 margin:0 auto; 的样式能让元素水平居中,而 margin: auto; 却不能做到垂直居中……直到现在.但 ...

  6. Hadoop面试链接

    http://blog.csdn.net/haohaixingyun/article/details/52819457 http://blog.csdn.net/kingmax54212008/art ...

  7. HDU 1256 画8 (找规律)

    题目链接 Problem Description 谁画8画的好,画的快,今后就发的快,学业发达,事业发达,祝大家发,发,发.   Input 输入的第一行为一个整数N,表示后面有N组数据. 每组数据中 ...

  8. 6.MySQL简介

    MySQL简介 ·点击查看MySQL官方网站 ·MySQL是一个关系型数据库管理系统,由瑞典MySQLAB公司开发,后来被Sun公司收购,Sun公司后来又被Oracle公司收购,目前属于facle旗下 ...

  9. arch优化开机

    查看开机时间 systemd-analyze 具体开机时间 systemd-analyze blame 你可以systemctl --all | grep not-found 查看有哪些服务挂掉了.然 ...

  10. 如何通过掩码计算可用的IP数量

    假设掩码是28,28也就是28个1.本身掩码是255.255.255.255那么转换成二进制也就是 11111111,11111111,11111111,11111111 那么28个1也就是: 111 ...