没有 XML 的 Spring AOP

发布于 2024-12-20 13:06:49 字数 452 浏览 1 评论 0原文

我正在尝试在没有任何 XML 的情况下设置 Spring AOP,并且想知道如何以这种方式启用自动代理。

定义一个 AutoProxyCreator-bean 是可行的,但是有没有更简单的方法呢?

这就是我的 @Configuration 的样子:

@Configuration
public class Context {
    @Bean
    public AnnotationAwareAspectJAutoProxyCreator annotationAwareAspectJAutoProxyCreator() {
        return new AnnotationAwareAspectJAutoProxyCreator();
    };
    ...
}

所有其他 bean 都由 AnnotationConfigApplicationContext 扫描。

I am trying to set up Spring AOP without any XML and wonder how to enable auto-proxying this way.

Defining an AutoProxyCreator-bean works, but isn't there an easier way?

This is what my @Configuration looks like:

@Configuration
public class Context {
    @Bean
    public AnnotationAwareAspectJAutoProxyCreator annotationAwareAspectJAutoProxyCreator() {
        return new AnnotationAwareAspectJAutoProxyCreator();
    };
    ...
}

All other beans are scanned in by AnnotationConfigApplicationContext.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亚希 2024-12-27 13:06:49

Spring 3.0.x 没有提供简单的方法来替换 @Configuration 中的 XML 命名空间扩展(例如 )。

即将推出的 Spring 3.1 将支持用于此目的的特殊注释,例如 @EnableAspectJAutoProxy

Spring 3.0.x doesn't provide easy ways to replace XML namespace extensions (such as <aop:aspectj-autoproxy>) in @Configuration.

An upcoming Spring 3.1 will support special annotations for this purpose, such as @EnableAspectJAutoProxy.

起风了 2024-12-27 13:06:49

最后,我找到了一种美观的方式来添加 AnnotationAwareAspectJAutoProxyCreator:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(AnnotationAwareAspectJAutoProxyCreator.class);
context.scan("com.myDomain");
context.refresh();

Finally I found an aesthetically pleasing way to add the AnnotationAwareAspectJAutoProxyCreator:

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(AnnotationAwareAspectJAutoProxyCreator.class);
context.scan("com.myDomain");
context.refresh();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文