Spring 3 Java Config:导入的@Configuration未增强?

发布于 2024-08-17 16:33:31 字数 2011 浏览 4 评论 0原文

我有一个应用程序,我试图将最新的 Spring 3 约定与基于注释的配置结合使用,但我也有使用构造函数注入的遗留类。根据 Spring 文档,您应该只调用创建该依赖项的方法并将其传递给需要它的构造函数。当 Configuration 类被增强时,这似乎有效,但如果您@Import Configuration 类,它似乎不会被增强,因此它可能会注入非自动装配的实例。

具体来说,如果我的一个存储库被构造函数注入到另一个 bean 中,并且该存储库与另一个 bean 在同一配置类中定义(我知道存储库应该位于它们自己的配置类中),那么它不会与注入的实体管理器。

我的 Beans:

public class MyBean {
    private ShouldBeSingleton d;
    public @Autowired MyBean(ShouldBeSingleton d) { this.d = d; }   
    public ShouldBeSingleton getMyDependency() { return d; }
}

public class ShouldBeSingleton { 
}

和这样的两个配置类:

@Configuration
public class MyImportedConfig { 
    @Bean public ShouldBeSingleton mySingleton() {
        return new ShouldBeSingleton();
    }       
    @Bean public MyBean myBean() {
        return new MyBean(mySingleton());
    }       
}

@Configuration
@Import({MyImportedConfig.class})
public class MyConfig {
}

我的 main 看起来像这样:

public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
    MyBean bean = context.getBean(MyBean.class);
    System.out.println("myInjectedSingleton=" + bean.getMyDependency());
    System.out.println("mySingleton=" + context.getBean(ShouldBeSingleton.class));
}

如果我加载 MyImportedConfig.class,我会得到正确的输出(即,如果我从 MyImportedConfig 中调用该方法,单例类是相同的):

myInjectedSingleton=test.ShouldBeSingleton@b42cbf
mySingleton=test.ShouldBeSingleton@b42cbf

但是,如果我更改 AnnotationConfigApplicationContext 以加载导入 MyImportedConfig 的 MyConfig,则会发生这种情况:

myInjectedSingleton=test.ShouldBeSingleton@a9ae05
mySingleton=test.ShouldBeSingleton@1dff3a2

这是预期的行为吗?我当前的解决方法是将需要将 EntityManager 注入到另一个 Configuration 类中的任何内容,导入它,然后在需要构造函数注入它的 Configuration 类中设置 @Autowired 依赖项。

我将其加载到 AnnotationConfigWebApplicationContext 中,如果我在 contextConfigLocation 中指定每个配置类,那么所有配置类都会得到适当的增强,但这似乎没有使用“模块化”@Import 功能。

I have an app that I'm trying to use the latest Spring 3 conventions with annotation-based configurations, but I also have legacy classes that use constructor-injection. Based on the Spring documentation you're supposed to just call the method that creates that dependency and pass it into the constructor that needs it. That seems to work when the Configuration class is enhanced, but it would seem that if you @Import the Configuration class, it is not enhanced, so it can potentially inject non-autowired instances.

Specifically, if one of my repositories is constructor-injected into another bean and that repository is defined in the same Configuration class as the other bean (I know the Repositories should be in their own config class), then it is not wired with an injected EntityManager.

My Beans:

public class MyBean {
    private ShouldBeSingleton d;
    public @Autowired MyBean(ShouldBeSingleton d) { this.d = d; }   
    public ShouldBeSingleton getMyDependency() { return d; }
}

public class ShouldBeSingleton { 
}

And two configuration classes like this:

@Configuration
public class MyImportedConfig { 
    @Bean public ShouldBeSingleton mySingleton() {
        return new ShouldBeSingleton();
    }       
    @Bean public MyBean myBean() {
        return new MyBean(mySingleton());
    }       
}

@Configuration
@Import({MyImportedConfig.class})
public class MyConfig {
}

My main looks like this:

public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
    MyBean bean = context.getBean(MyBean.class);
    System.out.println("myInjectedSingleton=" + bean.getMyDependency());
    System.out.println("mySingleton=" + context.getBean(ShouldBeSingleton.class));
}

If I load the MyImportedConfig.class, I get the correct output (i.e. the singleton class is the same if I call the method from within the MyImportedConfig):

myInjectedSingleton=test.ShouldBeSingleton@b42cbf
mySingleton=test.ShouldBeSingleton@b42cbf

But, if I change the AnnotationConfigApplicationContext to load MyConfig which imports MyImportedConfig this happens:

myInjectedSingleton=test.ShouldBeSingleton@a9ae05
mySingleton=test.ShouldBeSingleton@1dff3a2

Is this expected behavior? My current workaround is to just move anything that requires an EntityManager to be injected to another Configuration class, importing it, and then setting an @Autowired dependency in the Configuration class that needs to constructor-inject it.

I'm loading this in a AnnotationConfigWebApplicationContext and if I specify each Configuration class in the contextConfigLocation, then all the Configuration classes are properly enhanced, but this doesn't seem to use the "modular" @Import functionality.

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

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

发布评论

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

评论(1

我不咬妳我踢妳 2024-08-24 16:33:31

所以...我猜这是一个错误?

So... I'm guessing this is a bug?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文