Spring @Configuration和< context:component-scan /> [英] Spring @Configuration and <context:component-scan />

查看:192
本文介绍了Spring @Configuration和< context:component-scan />的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景在嵌入式Jetty上配置Spring Security 如果我使用JavaConfig配置Jetty服务器,这似乎有点解决。

I have a scenario configuring Spring Security on embedded Jetty which seems to be somewhat solved if I make use of JavaConfig to configure the Jetty server.

因此,它看起来像JavaConfig而不是XML可能是更好的选择对于项目的大块。但是,XML命名空间中有一些细节,例如< context:component-scan /> ,这些细节在中不可用配置设置。

As a result, it's looking like JavaConfig rather than XML might be the better option for large chunks of the project. However, there are some niceties in the XML namespaces, like <context:component-scan /> which aren't readily available in a @Configuration setting.

我发现 ApplicationContextAware 荣幸为 @Configuration 类,所以可以使用以下内容

I have discovered that ApplicationContextAware is honored for @Configuration classes, so the following is possible

@Configuration
public class FooConfig implements ApplicationContextAware {
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        ((AnnotationConfigApplicationContext) applicationContext).scan("org.example");
    }
}

替代方案,记录,是 @Configuration class使用 @ImportResource 注释并提取现有的XML文件:

The alternative, which is documented, is to have the @Configuration class use an @ImportResource annotation and pull in an existing XML file:

@Configuration
@ImportResource("applicationContext-withComponentScan.xml")
public class BarConfig {}

我想问题是以这种方式滥用 ApplicationContextAware 这是不好的形式,还是真的不是滥用?对于这种方法有点奇怪,所以如果春天的人以某种方式覆盖了我没有发现的东西,我也不会感到惊讶。

I guess the question is "Is it bad form to abuse ApplicationContextAware in this way, or is it really not abuse"? Something just feels oddly dirty about the approach so I'd not be surprised if the Spring guys had covered this in some way or another that I've not spotted.

感兴趣的是,问题涉及用 @Resource @Provider 扫描泽西设置,我宁可不要必须手动管理类/ XML配置中的条目。

For the interested, the problem relates to scanning a Jersey setup with @Resource and @Provider classes that I'd rather not have to manually manage entries in a class/XML configuration.

推荐答案


这是不好的滥用形式ApplicationContextAware以这种方式,或者它真的没有滥用

Is it bad form to abuse ApplicationContextAware in this way, or is it really not abuse

是的,这是不好的形式。如果您要手动取出上下文中的内容,您可能不会首先考虑依赖注入。

Yes, this is bad form. If you're going to fetch things out of the context manually, you may as well not bother with dependency injection in the first place.

然而,您的第二个选项( @ImportResource(applicationContext-withComponentScan.xml))是一个很好的例子 - 当你想要将这些XML宏与注释式配置结合使用时,这是当前的最佳实践。

However, your second option (@ImportResource("applicationContext-withComponentScan.xml")) is a good one - this is current best practice when you want to use these XML macros in combination with annotation-style config.

第三个选项是使用Spring 3.1的当前里程碑版本,使用 @Feature 添加了一种用Java完成这些操作的方法。但是,这还没有准备就绪。

A third option is to use the current milestone build of Spring 3.1, which adds a way of doing these things all in Java, using @Feature. This is not yet production-ready, though.

这篇关于Spring @Configuration和&lt; context:component-scan /&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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