Spring - 无法获得简单的方面工作

时间:2013-07-26 05:27:41

标签: spring spring-mvc aop aspectj

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
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";
    }
}

1 个答案:

答案 0 :(得分:1)

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

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

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