简体   繁体   English

AspectJ autoproxy与Spring Controllers和Webflow Actions有关

[英]AspectJ autoproxy issues with Spring Controllers and Webflow Actions

I have two related issues regarding spring/AspectJ AOP. 我有关于spring / AspectJ AOP的两个相关问题。 I have a typical logger aspect which logs exceptions thrown from any class in my application including services, daos, controllers and webflow actions... 我有一个典型的记录器方面,记录从我的应用程序中的任何类抛出的异常,包括服务,daos,控制器和webflow动作......

@Aspect
public class AspectLogger {

    @AfterThrowing(pointcut = "execution(* com.myapp..*.*(..))", throwing = "t")
    public void logGustavoException(JoinPoint joinPoint, Throwable t) {

        Log logger = LogFactory.getLog(joinPoint.getTarget().getClass());
        logger.error(t.getMessage(), t);
    }
}

In my application context I have an equally typical configuration... 在我的应用程序环境中,我有一个同样典型的配置

<context:annotation-config />

<!-- AOP logging config --> 
<aop:aspectj-autoproxy>
    <aop:include name="aspectLogger" />
</aop:aspectj-autoproxy>

<bean id="aspectLogger" class="com.myapp.AspectLogger" />

This works fine for the most part, the issue I have is with the webflow actions and controllers which implement an interface. 这在大多数情况下工作正常,我遇到的问题是webflow操作和实现接口的控制器。

1 - Controllers which implement an Interface... 1 - 实现接口的控制器......

One of our controllers implements an interface which defines one method, as well as defining several public methods which are used as @RequestMapping handlers... 我们的一个控制器实现了一个接口,它定义了一个方法,并定义了几个用作@RequestMapping处理程序的公共方法......

@Controller
public class AmazingController implements OutstandingInterface {

    // implements the method from OutstandingInterface
    @Override
    public Object doSomethingOutstanding(){
        ...
    }

    @RequestMapping(value="/amazingUrl.htm", method = RequestMethod.GET)
    public String doSomethingAmazing(HttpSession session, ModelMap model) {
        return "anAmazingViewName";
    }

    ...
}

The issue here is that due to the fact that the controller implements an interface that doesn't define all its public methods (ie controller request mapping methods), a proxy is created for the controller which only proxies the 'doSomethingOutstanding' method from OutstandingInterface. 这里的问题是,由于控制器实现了一个不定义其所有公共方法的接口(即控制器请求映射方法),因此为控制器创建了一个代理,该代理只代理OutstandingInterface中的'doSomethingOutstanding'方法。 As such, when a request comes in to /amazingUrl.htm, Spring does not route it to the appropriate request handler - it's as though the request mapping doesn't exist. 因此,当请求进入/amazingUrl.htm时,Spring不会将其路由到适当的请求处理程序 - 就好像请求映射不存在一样。 I have solved this by defining an interface for the controller which extends OutstandingInterface and also defines the request handler methods required by the controller, but it seems odd/wrong to me to have to define an interface for a controller just so that the AspectJ stuff doesn't 'hide' the request handler ... 我已经通过为控制器定义了一个接口来解决这个问题,该接口扩展了OutstandingInterface并且还定义了控制器所需的请求处理程序方法,但是我必须为控制器定义一个接口以便AspectJ的东西不会出现奇怪/错误。 't'隐藏'请求处理程序 ...

@Controller
public interface IAmazingController extends OutstandingInterface{

    @RequestMapping(value="/amazingUrl.htm", method = RequestMethod.GET)
    public String doSomethingAmazing(HttpSession session, ModelMap model);

}

...

public class AmazingController implements IAmazingController {

    @Override
    public Object doSomethingOutstanding(){
        ...
    }

    @Override
    @RequestMapping(value="/amazingUrl.htm", method = RequestMethod.GET)
    public String doSomethingAmazing(HttpSession session, ModelMap model) {
        return "anAmazingViewName";
    }

    ...
}

2 - Webflow Actions 2 - Webflow操作

The second issue is very similar. 第二个问题非常相似。 After introducing the AspectJ configuration, none of my webflow Action classes were being autowired correctly - I kept getting 'cannot find bean of type FantasticAction' sort of errors. 在介绍了AspectJ配置之后,我的webflow Action类都没有正确自动装配 - 我一直在'找不到类型为FantasticAction的bean'的错误。 Again, I introduced interfaces for all of the Action classes and this solved the problem as it was the proxy that was being injected at runtime, not the actual action implementation class. 我再次介绍了所有Action类的接口,这解决了问题,因为它是在运行时注入的代理,而不是实际的动作实现类。

So finally... the question in both instances is - is there a way of getting around these AspectJ issues without having to define interfaces for every class I want to advise? 所以最后......两个实例中的问题是 - 有没有办法解决这些AspectJ问题,而无需为我想建议的每个类定义接口?

You should add CGLIB dependendy in your class path so you will not need to create interfaces for working with AOP 您应该在类路径中添加CGLIB dependendy,这样您就不需要创建用于使用AOP的接口

Take a look to the doc . 看看文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 aspectj和spring with aspectj-autoproxy - aspectj and spring with aspectj-autoproxy 春天的aop:aspectj-autoproxy和tx:注释驱动的冲突 - aop:aspectj-autoproxy and tx:annotation-driven conflict in spring Spring 3.0 / AOP / Aspectj:autoproxy拦截对getConnection()的任何调用 - Spring 3.0 / AOP / Aspectj:autoproxy intercept any call to getConnection() 在春天可以对aspectj-autoproxy进行精细控制吗? - Is fine grained control of aspectj-autoproxy possible in spring? AspectJ 自动代理在 GigaSpaces 8 和 Spring 3 中失败,因为类是由不同的类加载器加载的 - AspectJ autoproxy fails with GigaSpaces 8 and Spring 3 because class is loaded by different classloader spring 仅为某些方面 bean 启用 aspectj-autoproxy - spring enable aspectj-autoproxy only for some aspect beans Spring因为proxy-target-class =“true”而无法自动装配<aop:aspectj-autoproxy /> - Spring Failed to Autowiring because of proxy-target-class=“true” and <aop:aspectj-autoproxy /> springs.xml中的错误,aspectj-autoproxy - Error in springs.xml , aspectj-autoproxy @Secured批注在具有Autoproxy的AspectJ模式下不起作用 - @Secured annotations not working in AspectJ Mode with Autoproxy 使用AspectJ的AOP无法使用<aop:aspectj-autoproxy /> - AOP using AspectJ not working with <aop:aspectj-autoproxy />
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM