2

错误

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>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${spring.framework.version}</version>
    </dependency>   

SERVLET-CONTEXT.XML

<aop:aspectj-autoproxy />

方面

@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
public interface DummyController {

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

}

控制器实施

@Controller
public class DummyControllerImpl implements DummyController {

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

1 回答 1

1

我认为这不再相关,但我遇到了完全相同的问题,解决方案是在应用程序上下文中设置它:

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

根本原因是,为此类生成的 AOP 自动代理是 JdkAutoProxy 而不是 CGLIB 生成的代理 - 这会导致 API 的 Spring MVC 调用失败,因为代理与控制器的类型不同。这个 JdkAutoProxy 的原因是因为这个特定的控制器实现了一个接口,这导致 spring 自动使用 JDK 自动代理。

于 2015-10-28T07:31:20.243 回答