2013-05-09 55 views
0

我使用Spring 3.1和APO(代理)。

提供用作Pointcat的註釋。在這種情況下Spring AOP的代理方法 「getMergeMappingsAndCals」 與CalendarMappingAnnotation註釋

我的建議是AfterReturning

看點:

@Component 
@Aspect 
public class MappingFilterAspect{ 
    /** 
    * 
    * @param retVal 
    */ 
    @AfterReturning(
     pointcut="@annotation(...annotation.CalendarMappingAnnotation)", 
     returning="retVal" 
    ) 
    public void calendarMappingFilter(Object retVal) { 


    } 
} 

譯註:

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface CalendarMappingAnnotation { 
} 

用法:

@Component 
public class ApoiMappingManagerImpl implements ApoiMappingManager, ApplicationContextAware, Serializable { 
    ... 
    @CalendarMappingAnnotation 
    public MergedMapAndCalsBeanCollection getMergeMappingsAndCals(){ 
     ... 
    } 
} 

配置:

<context:component-scan base-package="...aus.aspect" /> 
<aop:aspectj-autoproxy/> 

堆棧跟蹤:

java.lang.ClassCastException: $Proxy43 cannot be cast to ...mapping.cals.ApoiMappingManagerImpl 
[JVM ...] at ...helpers.SaveFillRestCalsClientHelper.init(SaveFillRestCalsClientHelper.java:62) 
[JVM ...] at ...DispatcherImpl.loadPlugin(DispatcherImpl.java:426) 
[JVM ...] at ...dispatcher.DispatcherImpl.run(DispatcherImpl.java:181) 
[JVM ...] at ...DispatcherImpl.complete(DispatcherImpl.java:319) 
[JVM ...] at ...DispatcherImpl.process(DispatcherImpl.java:259) 
[JVM ...] at ...RunnerImpl.run(RunnerImpl.java:88) 
[JVM ...] at ...JvmLauncherSlave.main(JvmLauncherSlave.java:40) 

回答

2

行爲看起來是正確的。

看起來您正在投射類型爲ApoiMappingManagerImpl的bean的實例,並且代碼正在拋出運行時異常。這是一個預期行爲的原因是因爲你使用的是Spring AOP,而Spring AOP本質上爲你創建了一個動態代理,所以這個代理基本上實現了接口ApoiMappingManager,在內部編寫了ApoiMappingManagerImpl並委託對這個組合類的調用。因此代理實現了ApoiMappingManager並且不擴展ApoiMappingManagerImpl並因此出現錯誤。

要解決它,你應該是如果需要轉換到接口而不是實現。

0

回答

這種問題的發生,因爲我不`噸描述接口這種方法這是由ApoiMappingManagerImpl

public interface ApoiMappingManager { 
    public MapBeanCollection assembleMaps(); 
    public String getRootFolder(); 
    public void setRootFolder(String rootFolder); 
    public ErrorList getErrorList(); 
    public Set<String> getAgentList(); 
    public Set<MapBean> getMappingSetByAgentName(String agentName); 
    public CalBeanCollection assembleCals(String dateStr); 
    public CalBeanCollection getCalBeanCollection(); 
    public MergedMapAndCalsBeanCollection getMergeMappingsAndCals(); // was missed 
} 
實施
相關問題