0

I use Spring 3.1 and APO(proxy).

Annotation which was provided used as Pointcat. In this case Spring AOP proxy method "getMergeMappingsAndCals" was annotated with CalendarMappingAnnotation

My advise is AfterReturning

Aspect:

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


    }
}

Annotation:

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

Usage:

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

Configuration:

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

Stacktrace:

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

2

The behavior looks correct.

It looks like you are casting an instance of a bean of type ApoiMappingManagerImpl and the code is throwing a runtime exception. The reason this is an expected behavior is because underlying you are using Spring AOP and Spring AOP essentially creates a dynamic proxy for you, this proxy essentially implements the interface ApoiMappingManager, internally composes the ApoiMappingManagerImpl and delegates calls to this composed class. So the proxy implements ApoiMappingManager and does NOT extend ApoiMappingManagerImpl and hence the error.

To fix it you should be if required casting to the interface not the implementation.

Sign up to request clarification or add additional context in comments.

Comments

0

Answer

This kind of issue happened because I don`t describe this method in interface which was implemented by 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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.