karthick
karthick

Reputation: 13

Spring AOP - Service methods not called when implementing spring aop

Aop configuration has been done in my project. Below configuration has been added for that purpose. The problem is when below code in not commented, methods in formService are not called. Hence i get null pointer exception. Any idea where the problem is ? I have attached the code below..

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"
    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 where formService bean is configured:

<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 Class :

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;
    }
}

Problem faced in below code :

Whenever i try to save a form in my application, formservice is not called. In below code, i have called createForm method of Formservice but it returns null..

    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;
    }

The sys out in createForm is not printed and I am terribly confused why this spring aop blocks the call of formservice method. When i comment that aop:config, everything works fine.

Upvotes: 1

Views: 1168

Answers (1)

M. Deinum
M. Deinum

Reputation: 124898

Your aspect is the culprit, it is effectively destroying the call. You aren't calling proceed() nor are return the result of that call to the caller. Hence your method is never invoked and you now effectively are always returning null.

Your method should be like this.

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

Upvotes: 4

Related Questions