0

我想在用@Controller 注释的类中选择所有用@RequestMapping 注释的方法。

以下切入点定义工作正常......在某些情况下:

@within(org.springframework.stereotype.Controller)
    and
@annotation(org.springframework.web.bind.annotation.RequestMapping)

问题是,一旦@Controller-annotated 类实现了某个接口,切入点就不再适用,并且方法不会被拦截。这甚至发生在我刚刚实现 java.io.Serializable 时。

例如,这不起作用,但只要我删除“ implements Serializable ”,它就会起作用:

@Controller
public class TestController extends BaseController implements Serializable {
    @RequestMapping(value = "/test")
    public String testAuth1(final Model model) {
        return "test";
    }
}

春季 XML 配置:

<aop:config>
    <aop:pointcut id="handlerMethods"
        expression="@within(org.springframework.stereotype.Controller) and @annotation(org.springframework.web.bind.annotation.RequestMapping)" />
    <aop:aspect ref="handlerAdvice">
        <aop:before method="interceptionMethod" pointcut-ref="handlerMethods" />
    </aop:aspect>
</aop:config>

有什么想法吗?谢谢!

4

1 回答 1

1

是否BaseController实现任何接口?

似乎您正在创建一个 cglib 代理,直到您添加接口实现,所以更简单的尝试解决它是添加proxy-target-class = "true"<aop:config>

<aop:config proxy-target-class="true">
...
</aop:config>

如果要使用 JDK 代理,则需要将注释移动到接口,否则 spring mvc 将看不到它们。

于 2012-10-24T08:23:54.137 回答