Chapter 8. Combining configuration approaches


JavaConfig can be used in conjunction with any or all of Spring's other container configuration approaches. The question is when and where it's appropriate to do so.

8.1. JavaConfig and XML

8.1.1. Bootstrapping JavaConfig from XML with ConfigurationPostProcessor

Currently, to use JavaConfig and XML config together, the configuration needs to be 'XML-driven', meaning that it will be the XML configuration that bootstraps JavaConfig.

   <!-- first, define your individual @Configuration classes as beans -->
   <bean class="com.myapp.config.AppConfig"/>
   <bean class="com.myapp.config.DataConfig"/>

    <!-- be sure to include the JavaConfig bean post-processor -->
    <bean class=""/>

Then, bootstrap an XML ApplicationContext:

ApplicationContext context = new ClassPathXmlApplicationContext("application-config.xml");

The beans defined in AppConfig and DataConfig will be available via 'context'. Configuring configurations

An added benefit that comes along with bootstrapping JavaConfig from XML is that the configuration bean instances are eligible, just as any other bean, for configuration:

    <!-- a possible "configurable configuration" -->
    <bean class="">
        <property name="env" value="TESTING"/>
        <property name="monitoring" value="true"/>
        <property name="certificates" value="classpath:/META-INF/config/MyCompany.certs"/>
    <!-- JavaConfig post-processor -->
    <bean class=""/>