Is it possible to unproxy a Spring bean?

前端 未结 2 1691
时光取名叫无心
时光取名叫无心 2020-12-01 13:58

I have a Spring bean, let\'s say:

@TransactionAttribute(TransactionAttributeType.REQUIRED) 
public class AImpl implements A {

     public void setSomeDepend         


        
2条回答
  •  青春惊慌失措
    2020-12-01 14:45

    Try this:

    if(AopUtils.isAopProxy(a) && a instanceof Advised) {
        Object target = ((Advised)a).getTargetSource().getTarget();
        AImpl ai = (AImpl)target;
    }
    

    Bonus: in Scala I am using the following equivalent function for the very same purpose:

    def unwrapProxy(a: AnyRef) = a match {
        case advised: Advised if(AopUtils.isAopProxy(advised)) => 
                                advised.getTargetSource.getTarget
        case notProxy => notProxy
    }
    

提交回复
热议问题