0

在浪费了很多时间试图找到答案之后,我决定发布这个疑问。
我有一个我想被 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 配置:

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

当我调用该方法ObjectToBeProxied.doStuff()时,ObjectToBeProxied.parameterValueService自动装配是可以的,而不是空的。

但是,当切面拦截方法调用ObjectToBeProxied.findEventParameterValue(..)并执行时ExceptionHandlerAspect.handleAround(..)ObjectToBeProxied.parameterValueService就不行了,它是空的。

调试它,我可以发现 Spring AspectObjectToBeProxied在拦截它后返回代理,但没有自动装配的属性对象。

我哪里错了?

4

0 回答 0