【问题标题】:@DeclareMixin with Spring AOP?@DeclareMixin 与 Spring AOP?
【发布时间】:2014-01-30 03:54:22
【问题描述】:

可以在 Spring AOP 中使用 @DeclareMixin 吗?还是他们只支持@DeclareParents?

我想编写一个方面,将 java.beans.PropertyChangeSupport 混合到一个 java bean 中:

public class PropertyChangeSupportWithInterface extends PropertyChangeSupport
    implements IObservableBean {

  public PropertyChangeSupportWithInterface(Object sourceBean) {
    super(sourceBean);
  }    
}

(IObservableBean 仅包含来自 PropertyChangeSupport 的所有公共方法)

@Aspect
@Named
public class ObservableAspect{
    @DeclareMixin("@ObservableBean *")
    public static IObservableBean createDelegate(Object object) {
      return new PropertyChangeSupportWithInterface(object);
    }
}

好像从来没有使用过这个方面,这让我觉得Spring AOP所做的运行时编织不支持@DeclareMixin。

有什么方法可以让它与 Spring AOP 一起工作?

您可以在此处找到一个(未运行的)示例(Maven 多模块项目):

https://github.com/BernhardBln/SpringAOPObservableBean

查看 springaop-observable-bean-aspect 子模块中的(唯一)测试用例。

【问题讨论】:

    标签: java aop mixins spring-aop


    【解决方案1】:

    不,Spring AOP 不支持它。我看到两个选项:

    • 为 Spring AOP 创建一个DeclareMixinIntroductionInterceptor
    • 切换到 Aspectj

    我认为PropertyChange 接口更适合 Aspectj,因为通常您会为原型 bean 创建大量代理,并且可以在框架之外轻松创建它们,例如通过 ORM。

    编辑

    不过我也对这个功能感兴趣,而且我已经做了一些使用它的工作:

    要使用它,您只需要声明一个DeclareMixinAutoProxyCreatorConfigurer 类型的bean 用于配置AnnotationAwareAspectJAutoProxyCreator 和上面的AdvisorFactory

    我只是在测试,但看起来效果很好。

    【讨论】:

    • 嘿何塞,非常感谢您的代码!但是如何在 Spring 中使用拦截器呢?我正在使用基于注释的处理。还是只能通过xml来完成?
    猜你喜欢
    • 2010-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多