简体   繁体   English

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

[英]Spring 3 Java Config: Imported @Configuration not enhanced?

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. 我有一个应用程序,我正在尝试使用基于注释的配置的最新的Spring 3约定,但我也有使用构造函数注入的遗留类。 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. 基于Spring文档,您应该只调用创建该依赖项的方法并将其传递给需要它的构造函数。 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. 这似乎在Configuration类增强时起作用,但是如果你@Inmport配置类,它似乎没有增强,所以它可能会注入非自动装配的实例。

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. 具体来说,如果我的一个存储库被构造函数注入到另一个bean中,并且该存储库在与另一个bean相同的Configuration类中定义(我知道存储库应该在它们自己的配置类中),那么它不会被注入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 (ie the singleton class is the same if I call the method from within the MyImportedConfig): 如果我加载MyImportedConfig.class,我得到正确的输出(即如果我从MyImportedConfig中调用方法,单例类是相同的):

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

But, if I change the AnnotationConfigApplicationContext to load MyConfig which imports MyImportedConfig this happens: 但是,如果我更改AnnotationConfigApplicationContext以加载导入MyImportedConfig的MyConfig,则会发生以下情况:

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. 我目前的解决方法是移动任何需要将EntityManager注入另一个Configuration类,导入它,然后在Configuration类中设置需要构造函数注入它的@Autowired依赖项。

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. 我在AnnotationConfigWebApplicationContext中加载它,如果我在contextConfigLocation中指定每个Configuration类,那么所有的Configuration类都得到了适当的增强,但这似乎没有使用“模块化”@Import功能。

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM