Spring AOP - 实现spring aop时未调用的服务方法

时间:2015-04-16 08:25:31

标签: java spring tomcat6 spring-aop

Aop配置已在我的项目中完成。为此目的添加了以下配置。问题是当下面的代码未注释时,不会调用formService中的方法。因此我得到空指针异常。知道问题出在哪里?我附上了以下代码..

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

    <bean id="aspectClass" class="com.unknown.aspect.AspectClass"></bean>

    <aop:config>
        <aop:aspect id="aspectId" ref="aspectClass">
            <aop:pointcut id="abcPointcut" expression="execution(* com.unknown.pat.service.impl.patServiceImpl.generatePrefixByAccountUnit(..))"/>
            <aop:pointcut id="billServicePointcut" expression="execution(* com.unknown.bill.service.impl.billServiceImpl.saveAdvancePayment(..))"/>
            <aop:around pointcut-ref="abcPointcut" method="getPrefixLogAround"/>
            <aop:after-returning pointcut-ref="billServicePointcut" returning="result" method="sequenceUpdateAfterReturning"/>
            <aop:after-throwing pointcut-ref="billServicePointcut" throwing="error" method="sequenceUpdateAfterThrowing"/>
        </aop:aspect>
    </aop:config>

</beans>

Application-Context-service.xml,其中配置了formService bean:

<bean id="formService" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces">
        <value>
            com.radaptive.rdpv.runtime.service.FormService
        </value>
    </property>
    <property name="target">
        <ref bean="formManager" />
    </property>
</bean>



<bean id="formManager" parent="txProxyTemplate">
    <property name="target">
        <bean class="com.radaptive.rdpv.runtime.service.impl.FormServiceImpl">
            <property name="services">
                <ref bean="services" />
            </property>
            <property name="messageResource">
                <ref bean="logResourceForService" />
            </property>

        </bean>
    </property>

    <property name="transactionAttributes">
        <props>

            <prop key="updateForm">
            </prop>
        </props>
    </property>
</bean>

Aspect类:

public class AspectClass {

    public void sequenceUpdateAfterReturning(JoinPoint joinPoint, Object result) throws RttException {
        WebApplicationContext ctx = (WebApplicationContext) RadaptiveApplicationCache.getInstance().getAttribute("SPRING_CONTEXT");
        Pat patientService = (Pat) ctx.getBean("pat");
        patientService.updatePrefixStatus("Success");
    }


    public void sequenceUpdateAfterThrowing(JoinPoint joinPoint, Throwable error) throws RttException {
        WebApplicationContext ctx = (WebApplicationContext) XXXApplicationCache.getInstance().getAttribute("SPRING_CONTEXT");
        Pat patientService = (Pat) ctx.getBean("pat");
        patientService.updatePrefixStatus("Failed");
    }

    public String getPrefixLogAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] paramValues = joinPoint.getArgs();
        String prefixStatus = String.valueOf(PrefixStatus.Reserved.getValue());
        //Get prefix method,waiting for till prefix status is reserved
        long startTime = System.currentTimeMillis();
        while(prefixStatus.equals(String.valueOf(PrefixStatus.Reserved.getValue()))){
            WebApplicationContext ctx = (WebApplicationContext) XXXApplicationCache.getInstance().getAttribute("SPRING_CONTEXT");
            Pat pat = (Pat) ctx.getBean("pat");
            prefixStatus = pat.getPrefixAvailability(paramValues[0].toString(),paramValues[1].toString(),paramValues[2].toString(), paramValues[4].toString());

            if(!prefixStatus.equals(String.valueOf(PrefixStatus.Reserved.getValue()))){
                Object retVal = joinPoint.proceed();
                prefixStatus = retVal.toString();
                break;
            }
        }
        return prefixStatus;
    }
}

以下代码遇到的问题:

每当我尝试在我的应用程序中保存表单时,都不会调用formservice。在下面的代码中,我调用createForm的{​​{1}}方法,但它返回null ..

Formservice

createForm中的sys out是而不是打印,我非常困惑为什么这个spring aop阻止了formservice方法的调用。当我评论 public String saveForm() throws RException { try { entityMap = ((FormService) services.get("formManager")).createForm(metaform.getFormName(), entityMap, userPrincipal, triggerContext); return SUCCESS_INCLUDE_DATA; } catch (Exception e) { } } public final Map<String,Object> createForm(final String formName, final Map values, final UserPrincipal userPrincipal, TriggerContext triggerContext) throws Exception { System.out.println("========== FORM SERVICE ENTER ======="); return formmap; } 时,一切正常。

1 个答案:

答案 0 :(得分:4)

你的方面是罪魁祸首,它有效地摧毁了这个电话。您没有调用proceed()也不会将该调用的结果返回给调用者。因此,您的方法永远不会被调用,现在您实际上总是返回null

你的方法应该是这样的。

public Object getPrefixLogAround(ProceedingJoinPoint joinPoint) throws Throwable {
    System.out.println("====333");
    return joinPoint.proceed();
}