Spring multiple @Configuration classes

后端 未结 3 898
后悔当初
后悔当初 2021-02-01 21:54

I am using Spring @Configuration annotation to configure my application.

Currently, I have a single @Configuration class where all my beans are

相关标签:
3条回答
  • 2021-02-01 22:00
    @Configuration
    @Import({ DataSourceConfig.class, TransactionConfig.class })
    public class AppConfig extends ConfigurationSupport {
          // bean definitions here can reference bean definitions in DataSourceConfig or TransactionConfig
    }
    
    0 讨论(0)
  • 2021-02-01 22:03

    Spring framework chapter-5 explained it very nicely.

    • @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.

    0 讨论(0)
  • 2021-02-01 22:21

    You should be able to autowire them:

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

    Alternatively, you can autowire beans rather than configurations:

    @Configuration 
    public class Conf2 {
        @Autowired
        Foo foo;
        ...
    }
    
    0 讨论(0)
提交回复
热议问题