使用AspectJ的Spring MVC [英] Spring MVC with AspectJ

查看:174
本文介绍了使用AspectJ的Spring MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作的spring mvc项目。我想使用AspectJ通过我的控制器记录每个请求。相关代码:

I have a working spring mvc project. I want to log each request through my controllers with using AspectJ. The relevant code:

控制器:(在包hu.freetime.controller中)

The Controller: (in package hu.freetime.controller)

@Controller
@RequestMapping("/")
public class BaseControllerImpl {
    @RequestMapping(method = RequestMethod.GET)
    public String index(Model model) {
        return "index";
    }
}

看点:

@Aspect
public class ControllerAspectImpl {
    Logger logger = LoggerFactory.getLogger(ControllerAspectImpl.class);

    @Pointcut("execution(public * hu.freetime.controller.BaseControllerImpl.*(..))")
    public void logController() {
    }


    @Around("logController()")
    public void log(final ProceedingJoinPoint pjp) {
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        logger.info("Calling Controller method: " + method.getName() + "()");
        try {
            pjp.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

WebAppInitializer:

The WebAppInitializer:

public class CashflowWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[] { RootConfig.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[] { WebConfig.class };
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }
}

WebConfig类:

The WebConfig class:

@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy
@ComponentScan(basePackages = { "hu.freetime.controller", "hu.freetime.aspect" })
public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    @Bean
    public ControllerAspectImpl getControllerAspect() {
        return new ControllerAspectImpl();
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

问题是,两个组件中只有一个工作中。如果我关闭AOP,MVC工作正常,但当我打开,我想要进入主页面时,我收到此错误:

The problem is, that only one of the two components is working. If I "turn off" the AOP, the MVC works perfect, but when i "turn on", and I want to go to the main page, I get this error:

HTTP Status 404 - .../WEB-INF/views/.jsp
The requested resource is not available.

奇怪的是它想要映射.jsp而不是index.jsp ,正如我在Controller的index()方法中所写的那样。我在运行时调试了,它确实停在了控制器方法。

The strange thing is that it wants to map the ".jsp" instead of "index.jsp", as I wrote in the Controller's index() method. I debugged at runtime, and it did stop at the controller method.

我怎样才能使它工作?

推荐答案

你的建议不会返回pjp.proceed()的结果。这是建议方法的返回值,必须由建议返回!否则您也将建议的方法转换为空格。

Your around advice does not return the result of pjp.proceed(). That is the return value of the advised method and must be returned by the advice! Otherwise you are turning the advised method into a void as well.

public **Object** log(final ProceedingJoinPoint pjp) {
...
    try {
        **return** pjp.proceed(); <<< !
    } catch (Throwable e) {
        e.printStackTrace();
    }    
}

这篇关于使用AspectJ的Spring MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆