0

After a lot of time waste trying to find a answer, I decided to post this doubt.
I have a class that I would like to intercept by the 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 AOP configuration:

<?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?

  • I do not think you need to use AopContext.currentProxy(). Do you have a good reason to do that? If not, remove getSelfRef() method and tell me what happens. – Angad Oct 24 '14 at 14:43
  • Get rid of expose-proxy="true" in your aspectj config as well – Angad Oct 24 '14 at 14:53
  • I'm following thoses advices http://stackoverflow.com/a/9223740/2765583 and http://www.intertech.com/Blog/secrets-of-the-spring-aop-proxy/. But following your advice, the aspect doesn't work normally, the `ExceptionHandlerAspect.handleAround(..)` was not triggered. – heber gentilin Oct 24 '14 at 15:57
  • I can note that, note only the autowired fields are losing reference, but all the other fields, like as the `AopContext.currentProxy()` reset the object or return a close of it. – heber gentilin Oct 27 '14 at 13:18

0 Answers0