java Spring 多个@Configuration 类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10698669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:09:56  来源:igfitidea点击:

Spring multiple @Configuration classes

javaspring

提问by chrisnfoneur

I am using Spring @Configurationannotation to configure my application.

我正在使用 Spring@Configuration注释来配置我的应用程序。

Currently, I have a single @Configurationclass where all my beans are declared. As the number of beans is growing (more than 30), I want to split it in many classes.

目前,我有一个@Configuration类,其中声明了我的所有 bean。随着 bean 的数量不断增加(超过 30 个),我想将其拆分为多个类。

Some beans are using common classes (mainly utility classes) :

一些 bean 使用公共类(主要是实用程序类):

Foo.classis an utility class Bar.classand Baz.classboth use Foo.class

Foo.class是一个实用类 Bar.classBaz.class都使用Foo.class

I want to have all Foo, Bar and Bazin three distinct @Configurationclasses (respectively Conf1, Conf2 and Conf3)

我想拥有Foo, Bar and Baz三个不同的@Configuration类(分别Conf1, Conf2 and Conf3

The problem is that I don't have access to an instance of Conf1from Conf2 and Conf3:

问题是我无权访问Conf1from的实例Conf2 and Conf3

Conf1.class

Conf1.class

@Configuration 
public class Conf1 {
    @Bean
    public Foo foo() {
        return new Foo();
    }
}

Conf2.class

Conf2.class

@Configuration 
public class Conf2 {
    @Bean
    public Bar bar() {
        Bar bar = new Bar();
        bar.setFoo(conf1.foo()); // Not possible !
        return bar;
    }
}

Conf3.class

Conf3.class

@Configuration 
public class Conf3 {
    @Bean
    public Baz baz() {
        Baz baz = new Baz();
        baz.setFoo(conf1.foo()); // Not possible !
        return baz;
    }
}

Any idea on how can I solve this issue ?

关于如何解决这个问题的任何想法?

回答by axtavt

You should be able to autowire them:

您应该能够自动装配它们:

@Configuration 
public class Conf2 {
    @Autowired
    Conf1 conf1;
    ...
}

Alternatively, you can autowire beans rather than configurations:

或者,您可以自动装配 bean 而不是配置:

@Configuration 
public class Conf2 {
    @Autowired
    Foo foo;
    ...
}

回答by jujadhav

@Configuration
@Import({ DataSourceConfig.class, TransactionConfig.class })
public class AppConfig extends ConfigurationSupport {
      // bean definitions here can reference bean definitions in DataSourceConfig or TransactionConfig
}

回答by Ramesh Papaganti

Spring framework chapter-5explained it very nicely.

Spring 框架第 5 章很好地解释了它。

  • @ExternalBean: One configuration class may need to reference a bean defined in another configuration class (or in XML, for that matter). The @ExternalBean annotation provides just such a mechanism. When JavaConfig encounters a method annotated as @ExternalBean, it replaces that method definition with a lookup to the enclosing bean factory for a bean with the same name as the method name

  • @Import: @Import represents JavaConfig's equivalent of XML configuration's element. One configuration class can import any number of other configuration classes, and their bean definitions will be processed as if locally defined

  • ConfigurationSupport: As a convenience, @Configuration classses can extend ConfigurationSupport, primarily in order to facilitate easy lookup of beans from the enclosing BeanFactory / ApplicationContext.

  • @ExternalBean:一个配置类可能需要引用在另一个配置类(或在 XML 中,就此而言)中定义的 bean。@ExternalBean 注解提供了这样一种机制。当 JavaConfig 遇到注释为 @ExternalBean 的方法时,它会通过查找封闭的 bean 工厂来替换该方法定义,以查找与方法名称同名的 bean

  • @Import:@Import 表示 JavaConfig 相当于 XML 配置的元素。一个配置类可以导入任意数量的其他配置类,并且它们的 bean 定义将像本地定义一样进行处理

  • ConfigurationSupport:为了方便起见,@Configuration 类可以扩展 ConfigurationSupport,主要是为了方便从封闭的 BeanFactory / ApplicationContext 中轻松查找 bean。