Spring AOP没有使用MVC

时间:2014-09-15 19:14:19

标签: java spring spring-mvc aspectj

我知道这个问题有很多线索,我试图通过所有的答案,但到目前为止没有任何工作。我的代码不会抛出任何错误,网页已启动并正常工作,但控件永远不会出现方面方法。这是我的Controller类:

@Controller
public class MyController {
    @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    @RequiresAuthenticatedUser
    public ModelAndView execute(final HttpServletRequest request) {
        System.out.println("welcome!");
        // generates model and view
        return mav;
    }
}

方面 -

@Aspect
@Component
public class RequiresAuthenticatedUserAspect {
    @Around("@annotation(com.mypkg.RequiresAuthenticatedUser)")
    public void validateUser(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object[] args = proceedingJoinPoint.getArgs();
        throw new Exception("Unauthorized user");
    }    
}

应用程序上下文 -

<!-- Enable AspectJ style of Spring AOP -->
<aop:aspectj-autoproxy proxy-target-class="false">
    <aop:include name="requiresAuthenticatedUser" />
</aop:aspectj-autoproxy>

请帮忙。

1 个答案:

答案 0 :(得分:0)

首先,春季和春季mvc hava的背景不同。 这意味着spring上下文保存了MyController和RequiresAuthenticatedUserAspect的bean,然后spring mvc context持有bean MyController。小心它是不同的。 当您向服务器请求时,Spring mvc上下文中的MyController负责处理此问题。此时RequiresAuthenticatedUserAspect在spring上下文中剪切了MyController。所以这方面永远不会奏效。

所以我认为你应该检查你的dispatcher-servlert.xml(可能是其他名字),你应该扫描包中的RequiresAuthenticatedUserAspect并注意你也应该扫描组件注释。