spring aop 同一个类中一个方法调用另一个方法,拦截不到被调用的方法

转自:http://huqilong.blog.51cto.com/53638/732088

 


 

       在开发基于 Spring 的应用的过程中碰到了一个让我困惑了好久的问题,我在一个 Service 类的 doSomething1() 方法中通过
this.doSomething2(); 语句调用了同一个类中的 doSomething2 方法,运行时通过调试发现 doSomething1 方法的执行前后正常地执行了自定义的 around 装备,但是在 doSomething2 方法执行前后并未如我所期望的那样执行自定义的 aroundadvice 。今天终于恍然大悟,把它当作笔记写下来。Spring 的代理实现有两种:一是基于 JDK Dynamic Proxy 技术而实现的;二是基于 CGLIB 技术而实现的。今天的目标是探索基于 JDK Dynamic Proxy 的动态代理。首先来看看如何自己动手实现一个对Service Bean 对象的动态代理。为了能够更清楚地看到 Spring AOP 动态代理的本质,我决定不使用 JDK 中提供的 Dynamic Proxy API,就使用最普通的 java 代码来模拟一个动态代理实例。

首先,来创建一个需要代理的接口:

 

packagedemo.interf;   

publicinterface ICustomerService { 

    public void doSomething1(); 

    public void doSomething2(); 

 然后就是具体服务类:

 

packagedemo.interf.impl; 

 

importdemo.interf.ICustomerService; 

 

public classCustomerServiceImpl implements ICustomerService { 

 

    public void doSomething1() { 

 

        System.out.println("InsideCustomerServiceImpl.doSomething1()"); 

 

        doSomething2(); 

 

    } 

 

    public void doSomething2() { 

 

        System.out.println("InsideCustomerServiceImpl.doSomething2()"); 

 

    } 

 

 下面我们就来模拟动态生成代理类的过程,若使用 JDK Dynamic Proxy,这一过程是在运行时进行的。

CustomerServiceImpl 类对象的代理类: 

 

package demo.interf.impl; 

 

import demo.interf.ICustomerService; 

 

public class CustomerServiceProxy implements ICustomerService { 

 

    private ICustomerServicecustomerService; 

 

    public voidsetCustomerService(ICustomerService customerService) { 

        this.customerService =customerService; 

    } 

 

    public void doSomething1(){ 

        doBefore(); 

       customerService.doSomething1();  

        doAfter(); 

    } 

 

    public void doSomething2(){ 

        doBefore(); 

       customerService.doSomething2(); 

        doAfter(); 

    } 

 

    private void doBefore() { 

        // 例如,可以在此处开启事务 

        System.out.println("dosome important things before..."); 

    } 

 

    private void doAfter() { 

        // 例如,可以在此处提交或回滚事务、释放资源等等 

        System.out.println("dosome important things after..."); 

    } 

 

 使用代理对象调用业务逻辑操作的客户端程序:

 

package test; 

 

import demo.interf.ICustomerService; 

import demo.interf.impl.CustomerServiceImpl; 

import demo.interf.impl.CustomerServiceProxy; 

 

public class TestProxy { 

     

    public static void main(String[]args) { 

        // 创建代理目标对象。对于Spring来说,这一工作 

        // 是由Spring DI容器完成的。 

        ICustomerServiceserviceProxyTarget = new CustomerServiceImpl(); 

 

        // 创建代理对象。对于Spring来说,这一工作 

        // 也是由Spring DI容器完成的。 

        CustomerServiceProxy serviceProxy= new CustomerServiceProxy(); 

       serviceProxy.setCustomerService(serviceProxyTarget); 

        ICustomerService serviceBean= (ICustomerService) serviceProxy; 

 

        // 调用业务逻辑操作 

       serviceBean.doSomething1(); 

    } 

 好了,完成了。现在以调试方式运行这个应用,你会发现在doSomething1() 中调用doSomething2() 方法的时候并未去执行CustomerServiceProxy类的doBefore()、doAfter()方法。再来看看这句关键代码:doSomething2();把它隐含的意思也表达出来吧:this.doSomething2();哦,我明白了,在CustomerServiceImpl 类中this 关键字表示的是当前这个CustomerServiceImpl类的实例。那程序当然就会去执行CustomerServiceImpl 类中的doSomething2() 方法了,而不会去执行CustomerServiceProxy 类中的doSomething2() 方法!!

在使用Spring AOP 的时候,我们从 IOC容器中获取的Service Bean 对象其实都是代理对象,而不是那些Service Bean 对象本身,也就是说获取的并不是被代理对象或代理目标。当我在自己的Service 类中使用this 关键字嵌套调用同类中的其他方法时,由于this 关键字引用的并不是该Service Bean 对象的代理对象,而是其本身,故Spring AOP 是不能拦截到这些被嵌套调用的方法的。

 



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
package com.spring.dao; import org.springframework.stereotype.Component; @Component("userDAO") public class UserDao { public void say() { System.out.println("say method is called"); } public void smile() { System.out.println("smile method is called"); } public void cry() { System.out.println("cry method is called"); } public void jump() { System.out.println("jump method is called"); } } 注意观察包名。@Component("userDAO")等价于在spring配置文件中定义一个<bean id="userDAO"/> 编写Service package com.spring.service; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.spring.dao.UserDao; @Component("userService") public class UserService { @Resource(name="userDAO") private UserDao dao; public UserDao getDao() { return dao; } public void setDao(UserDao dao) { this.dao = dao; } public void say() { dao.say(); } public void smile() { dao.smile(); } public void cry() { dao.cry(); } public void jump() { dao.jump(); } } 注意观察包名。@Component("userService")等价于在spring配置文件中定义一个<bean id="userService"/> @Resource(name="userDAO")将userDA注入进来 写一个拦截器的类 package com.spring.aop; import org.springframework.stereotype.Component; @Component("logIntercepter") public class LogIntercepter { public void before(){ System.out.println("----------before-------------"); } public void after(){ System.out.println("----------after-------------"); } public void exception(){ System.out.println("----------exception-------------"); } public void around(){ System.out.println("----------exception-------------"); } } 注意观察包名。@Component("logIntercepter")等价于在spring配置文件中定义一个<bean id="logIntercepter"/> applicationContext.xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:annotation-config/> <context:component-scan base-package="com.spring.*"/> <aop:config> <aop:aspect id="aspect" ref="logIntercepter"> <aop:pointcut expression="execution(* com.spring.service..*(..))" id="pointCut"/> <aop:before method="before" pointcut-ref="pointCut"/> <aop:after method="after" pointcut-ref="pointCut"/> <aop:after-throwing method="exception" pointcut-ref="pointCut"/> <!-- <aop:around method="around" pointcut-ref="pointCut"/> --> </aop:aspect> </aop:config> </beans><context:annotation-config/> <context:component-scan base-package="com.spring.*"/> 两行为开启spring的注解配置 <aop:aspect id="aspect" ref="logIntercepter"> 引入具体的AOP操作类 <aop:pointcut expression="execution(* com.spring.service..*(..))" id="pointCut"/>声明一个切入点,注意execution表达式的写法 <aop:before method="before" pointcut-ref="pointCut"/> aop前置通知 <aop:after method="after" pointcut-ref="pointCut"/> aop后置通知, <aop:after-throwing method="exception" pointcut-ref="pointCut"/> aop异常通知 以上结合起来意思就是在调用com.spring.service包或子包下的所有方法之前或之后或抛出异常时依次调用id为logIntercepter的类中的before after exception方法 测试用例 package com.spring.test; import javax.annotation.Resource; import org.junit.Test; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; import com.spring.service.UserService; @ContextConfiguration(locations="classpath:applicationContext.xml") public class SpringTest extends AbstractJUnit4SpringContextTests { @Resource(name="userService") private UserService userService; @Test public void test1(){ userService.say(); System.out.println(); userService.smile(); System.out.println(); userService.cry(); } } 此单元测试基于spring的AbstractJUnit4SpringContextTests,你需要添加spring的关于单元测试的支持 在类上标注@ContextConfiguration(locations="classpath:applicationContext.xml")意思是去classpath路径下加载applicationContext.xml @Resource(name="userService")意思是把userService注入进来 最终输出结果为: ----------before------------- say method is called ----------after------------- ----------before------------- smile method is called ----------after------------- ----------before------------- cry method is called ----------after-------------

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值