Chapter 8. Combining configuration approaches

Abstract

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.

<beans>
   <!-- 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="org.springframework.config.java.process.ConfigurationPostProcessor"/>
</beans>

Then, bootstrap an XML ApplicationContext:

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

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

8.1.1.1. 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:

<beans>
    <!-- a possible "configurable configuration" -->
    <bean class="org.my.company.config.AppConfiguration">
        <property name="env" value="TESTING"/>
        <property name="monitoring" value="true"/>
        <property name="certificates" value="classpath:/META-INF/config/MyCompany.certs"/>
    </bean>
    <!-- JavaConfig post-processor -->
    <bean class="org.springframework.config.java.process.ConfigurationPostProcessor"/>
</beans>