jax
jax

Reputation: 38573

Spring - can't get simple aspect working

ERROR

SEVERE: Servlet.service() for servlet [appServlet] in context with path [/callcentre] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class
HandlerMethod details: 
Controller [$Proxy109]
Method [public final java.lang.String au.com.mycompany.web.controllers.DummyControllerImpl.dummy()]
Resolved arguments: 
] with root cause
java.lang.IllegalArgumentException: object is not an instance of declaring class
...

MAVEN DEPENDENCY

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${spring.framework.version}</version>
    </dependency>   

SERVLET-CONTEXT.XML

<aop:aspectj-autoproxy />

ASPECT

@Aspect
@Component
public class JsonAspect {

    @Before("execution(public * au.com.mycompany.web.controllers.DummyController.dummy(..))")
    public final void beforeMethod1(final JoinPoint joinPoint) {
        System.out.println("Intercepted.............");
        System.out.println(joinPoint.getSignature().getName());
    }

}

CONTROLLER INTERFACE

@Controller
public interface DummyController {

    @RequestMapping(value = "/dummy", method = RequestMethod.GET)
    @ResponseBody
    String dummy();

}

CONTROLLER IMPL

@Controller
public class DummyControllerImpl implements DummyController {

    @Override
    public final String dummy() {
        System.out.println("IT WORKED..........");
        return "it returned";
    }
}

Upvotes: 2

Views: 651

Answers (1)

RonK
RonK

Reputation: 9652

I assume this is no longer relevant, but I had the exact same issue and the solution was to set this in the application context:

<aop:aspectj-autoproxy proxy-target-class="true"/>

Root cause, is that the AOP Auto Proxy generated for this class is a JdkAutoProxy instead of a CGLIB generated proxy - which causes a failure in the spring MVC invocation of the API since the proxy is not of the same type as the controller. The reason for this JdkAutoProxy is since this specific controller implements an interface, which causes spring to automatically use the JDK auto proxy.

Upvotes: 1

Related Questions