Потеря автоматических атрибутов после выполнения перехватчика из Spring AOP

После того, как я потратил много времени, пытаясь найти ответ, я решил опубликовать это сомнение.
У меня есть класс, который я хотел бы перехватить с помощью Spring AOP.

ObjectToBeProxied.java

package com.ee.beans; 

@Service
@Component
@Transactional
public ObjectToBeProxied implements IObjectToBeProxied {
    @Autowired
    private ParameterValueService parameterValueService;

    public void doStuff() {
        // do something before the call
        getSelfRef().findEventParameterValue(new ParameterValueFilter());
        // do something after
    }

    @HandleException
    private Boolean findEventParameterValue(ParameterValueFilter parameterValueFilter) {
        ParameterValue parameterValue = getSelfRef().parameterValueService.findParametertValueByFilter(parameterValueFilter);
        return parameterValue.value();
    }

    private ObjectToBeProxied getSelfRef() {
        return (ObjectToBeProxied) AopContext.currentProxy();
    }
}

ExceptionHandlerAspect.java

package com.ee.aspects;    

@Component
public class ExceptionHandlerAspect {

    private static Logger LOGGER = Logger.getLogger(ObjectToBeProxied.class);

    public Object handleAround(ProceedingJoinPoint joinPoint) throws Throwable {
        // Handling the exception. Need to continue either the method throws a expcetion
        // but it need to be logged
        try {
            return joinPoint.proceed();
        } catch (Exception e) {
            // something to handle the exception
        }
        return null;
    }
}

Конфигурация Spring АОП:

<?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"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

    <aop:aspectj-autoproxy expose-proxy="true" proxy-target-class="true"/>

    <!-- Activates various annotations to be detected in bean classes -->
    <context:annotation-config/>
    <context:spring-configured />

    <!-- Scans the classpath of this application for @Components to deploy as beans -->
    <context:component-scan base-package="com.ee.beans"/>

    <bean id="exceptionHandlerAspect" class="com.ee.aspects.ExceptionHandlerAspect" />

    <aop:config>
        <aop:aspect id="exceptionHandlerConfig" ref="exceptionHandlerAspect">

            <!-- Trata exceções lançadas -->
            <aop:pointcut id="exceptionHandlerAroundMethod" expression="execution(* com.ee.beans.ObjectToBeProxied.*(..)) &amp;&amp; @annotation(com.ee.exceptions.HandleException)" />
            <aop:around pointcut-ref="exceptionHandlerAroundMethod" method="handleAround" />
        </aop:aspect>
    </aop:config>

When I call the method ObjectToBeProxied.doStuff(), ObjectToBeProxied.parameterValueService autowired is ok, not null.

But, when the aspect intercept the method call ObjectToBeProxied.findEventParameterValue(..) and execute the ExceptionHandlerAspect.handleAround(..), the ObjectToBeProxied.parameterValueService is not ok, it's null.

Debugging it, I can figure out that the Spring Aspect return the ObjectToBeProxied proxy after intercept it, but without the autowired attributes objects.

Where am I getting wrong?


person heber gentilin    schedule 24.10.2014    source источник
comment
Я не думаю, что вам нужно использовать AopContext.currentProxy(). У вас есть для этого веская причина? Если нет, удалите метод getSelfRef() и сообщите мне, что происходит.   -  person Angad    schedule 24.10.2014
comment
Избавьтесь от expose-proxy=true в конфигурации вашего аспекта.   -  person Angad    schedule 24.10.2014
comment
Я следую этим советам stackoverflow.com/a/9223740/2765583 и intertech.com/Blog/secrets-of-the-spring-aop-proxy. Но по вашему совету аспект не работает нормально, ExceptionHandlerAspect.handleAround(..) не сработал.   -  person heber gentilin    schedule 24.10.2014
comment
Я могу отметить, что только автоматически связанные поля теряют ссылку, но все остальные поля, такие как AopContext.currentProxy(), сбрасывают объект или возвращают его закрытие.   -  person heber gentilin    schedule 27.10.2014