Open In App
Related Articles

How to generate spring XMP configuration files programmatically ?

Improve Article
Save Article
Like Article

Problem statement As we all know, in a spring application we provide configuration information through ‘ApplicationContext’. There are multiple classes provided by spring framework that implements this interface and helps us use configuration information in applications, and ClassPathXmlApplicationContext is one among them. Usually in this case all the bean definitions are configured in a single standalone xml file commonly termed as ‘application-context.xml’. In larger project structures, the best practice is to keep multiple spring configuration files for easy maintainability and modularity. In that case the best thing to do is to organize all your Spring bean configuration files into a single XML file, and import the entire files within it. Further, in the application context, we load this single xml file. Now what if the number of configuration files to be created is outsized? Say for example, close to 100 or more ? Now that’s a humdrum and snail-like job! It’s time to find a path to automate this process. Let’s think of a scenario where we have all the required data in an external source, say for instance, an excel sheet. Now we need to read data from the sheet and generate close to 100 spring configuration files spontaneously. How to automate this process : JAXB to the rescue. JAXB has a binding compiler. JAXB XJC schema binding compiler transforms, or binds, a source XML schema to a set of JAXB content classes in the Java programming language.

  1. Look for the xml schema that needs to be referred in the xml to be generated -
  2. Hit this url in browser to get the xsd. Now save it in some location (like spring.xsd).
  3. Download jaxbri package and the set the environment path variable .In case of maven projects, include dependencies for jaxb-ri and jaxb-core.
  4. Execute the command xjc.exe spring.xsd, from the folder where spring.xsd exists. This would generate the classes associated to the xsd.

What needs to be done if ‘Property is already defined ‘ error occurs while executing the command ? It is likely to get an exception that says ‘Property is already defined’, as shown in the below screenshot. If so, the issue can be resolved by creating a binding file, to override the properties for which the exception was thrown. exception Binding file spring25 Use below command to parse the schema and generate the classes:

xjc -b spring25.xjb -verbose -xmlschema

Generated classes org Now create a Bean instance and marshall it. This would generate xml file with the required output. Sample code Note:- Test class is created under org\springframework\schema\beans. 


package org.springframework.schema.beans;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import org.springframework.util.CollectionUtils;
import com.hrb.leap.bean.SpringConfigGenerator;
public class Test
    public static void main(String[] args) throws JAXBException
        ObjectFactory factory = new ObjectFactory();
        JAXBContext context = JAXBContext.newInstance("org.springframework.schema.beans");
        * Create the root element 'Beans'
        * Set the required schema properties
        Beans rootElement = factory.createBeans();
        rootElement.getOtherAttributes().put(new QName
        ("xmlns:xsi"), "");
        rootElement.getOtherAttributes().put(new QName
        " spring-beans-3.2.xsd");
        * How to import resources
        * How to define list of reference beans
        java.util.List<String> resources = new ArrayList<>();
        resources.forEach(resourceName ->
            Import importResource = new Import();
        Bean bean = factory.createBean();
        if (!CollectionUtils.isEmpty(resources))
            ConstructorArg constructorArgs = factory.createConstructorArg();
            org.springframework.schema.beans.List listOfResources =
                    new org.springframework.schema.beans.List();
            resources.forEach(referenceFormName ->
                Ref refBean = new Ref();
        * Sample bean definition to show how to store
        list of values as a property
            Bean simpleBean = factory.createBean();
            PropertyType firstProperty= factory.createPropertyType();
            java.util.List<String> listOfValues = new ArrayList<>();
            org.springframework.schema.beans.List listToStoreValues
                                = new org.springframework.schema.beans.List();
            listOfValues.forEach(name ->
                Value value = factory.createValue();
            //Add the property to the bean.
            // Add the bean under the root element 'beans'
            Marshaller marshaller = context.createMarshaller();
            createXmlConfiguration(marshaller , "output", rootElement);
    * Method create the xml under the 'src/main/resources'
    folder in the project.
    public static void createXmlConfiguration (Marshaller marshaller ,
                                String fileName, Beans rootElement)
   url = SpringConfigGenerator.class.getResource("/");
            File fullPathToSubfolder = new File(url.toURI()).getAbsoluteFile();
            String projectFolder =
            // TODO - Destination folder to be configured
            File outputFile = new File(projectFolder
                            + "src/main/resources/" + "/"+fileName+".xml");
            if (!outputFile.exists())
            OutputStream os = new FileOutputStream(outputFile);
            marshaller.marshal(rootElement, os);
        catch (URISyntaxException uriException)
        catch (IOException ioException)
        catch (JAXBException jaxbException)

Output Output Follow the below steps to see the sample output:

  1. Hit the schema url in browser to get the xsd. Now save it in some location (eg:- spring.xsd).
  2. Download jaxbri package and the set the environment path variable .
  3. Set classpath of the jaxb libraries and current path(dot) like example (do this command prompt or equivalent in IDE)
set classpath=C:\folder_name\jaxb-ri\lib;.;
  1. After extracting the attached to a folder run below command (from the extracted folder in cmd), compile the ‘Test’ class.
javac org\springframework\schema\beans\
  1. Finally run the program.
java org.springframework.schema.beans.Test
  1. Note:- Output will print in the console, if ‘createXmlConfiguration(marshaller , “output”, rootElement);’ is replaces by ‘marshaller.marshal(rootElement, System.out);’


  1.  Effort is reduced considerably. A single person might take minimum of 20-25 days to configure close to 85 files (considering 9 hours/day), it would account to 225 hours. The above tool would take less than 3 seconds to do so.
  2. If there are minor configuration changes to be done in future, we can take the specific file alone and make the corrections.

Alternative approach If the number of configuration files is very less, we can try loading the context with all the bean definitions on the fly, without generating the physical xml files. This can be achieved by loading the spring context programmatically, and run the application as and when required. Drawbacks 

  1.  Memory Consumption high
  2. Performance low
  3. Minor mistake in data source would have big impact.
  4. Risky to run in production environment, on the fly.
  5. Not an advised approach, if the data source remains almost untouched
  6. Will be difficult to troubleshoot.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 01 Nov, 2022
Like Article
Save Article
Similar Reads