gpt4 book ai didi

java - Spring AOP - 实现 spring aop 时不调用服务方法

转载 作者:行者123 更新时间:2023-11-29 03:10:27 27 4
gpt4 key购买 nike

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>

配置formService bean的Application-Context-service.xml:

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

方面类:

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

下面代码遇到的问题:

每当我尝试在我的应用程序中保存表单时,都不会调用表单服务。在下面的代码中,我调用了 FormservicecreateForm 方法,但它返回 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;
}

createForm 中的 sys out 打印,我非常困惑为什么这个 spring aop 会阻止 formservice 方法的调用。当我评论 aop:config 时,一切正常。

最佳答案

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

你的方法应该是这样的。

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

关于java - Spring AOP - 实现 spring aop 时不调用服务方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29669247/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com