Spring AOP - 实现 spring aop 时不调用服务方法
Spring AOP - Service methods not called when implementing 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>
看点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;
}
}
以下代码中遇到的问题:
每当我尝试在我的应用程序中保存表单时,都不会调用表单服务。在下面的代码中,我调用了 Formservice
的 createForm
方法,但它 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;
}
createForm 中的 sys out 是 not 打印的,我非常困惑为什么这个 spring aop 会阻止 formservice 方法的调用。当我评论 aop:config
时,一切正常。
你的方面是罪魁祸首,它正在有效地破坏电话。您不是在调用 proceed()
,return 也不是调用调用者的结果。因此,您的方法永远不会被调用,您现在实际上总是 returning null
.
你的方法应该是这样的
public Object getPrefixLogAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("====333");
return joinPoint.proceed();
}
我的项目已经完成了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>
看点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;
}
}
以下代码中遇到的问题:
每当我尝试在我的应用程序中保存表单时,都不会调用表单服务。在下面的代码中,我调用了 Formservice
的 createForm
方法,但它 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;
}
createForm 中的 sys out 是 not 打印的,我非常困惑为什么这个 spring aop 会阻止 formservice 方法的调用。当我评论 aop:config
时,一切正常。
你的方面是罪魁祸首,它正在有效地破坏电话。您不是在调用 proceed()
,return 也不是调用调用者的结果。因此,您的方法永远不会被调用,您现在实际上总是 returning null
.
你的方法应该是这样的
public Object getPrefixLogAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("====333");
return joinPoint.proceed();
}