Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring multiple @Configuration classes

Tags:

java

spring

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

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

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

Foo.class is an utility class Bar.class and Baz.class both use Foo.class

I want to have all Foo, Bar and Baz in three distinct @Configuration classes (respectively Conf1, Conf2 and Conf3)

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

Conf1.class

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

Conf2.class

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

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 ?

like image 540
chrisnfoneur Avatar asked May 22 '12 08:05

chrisnfoneur


People also ask

Can we have multiple configuration classes in Spring?

One configuration class can import any number of other configuration classes, and their bean definitions will be processed as if locally defined.

Can we use @component and @configuration in same class?

@Component Indicates that an annotated class is a "component". Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning. A @Configuration is also a @Component, but a @Component cannot act like a @Configuration.

How does @configuration work in Spring?

Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.

Can application have multiple configuration files?

For a large application, it is better to use multiple configuration file that one so that it may be easy to manage the application. We can create many configuration files but we need to define it in the struts. xml file. The includesub-element of struts is used to define the supported configuration file.


2 Answers

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;
    ...
}
like image 68
axtavt Avatar answered Oct 07 '22 15:10

axtavt


@Configuration
@Import({ DataSourceConfig.class, TransactionConfig.class })
public class AppConfig extends ConfigurationSupport {
      // bean definitions here can reference bean definitions in DataSourceConfig or TransactionConfig
}
like image 20
jujadhav Avatar answered Oct 07 '22 15:10

jujadhav