Does Spring Support JSON Configuration?

donalbain picture donalbain · Apr 19, 2012 · Viewed 7.2k times · Source

Does anyone know if Spring has any extensions that allow for configuring its ApplicationContext via JSON (or really any other format) rather than XML? I couldn't find anything in the official docs, but I was wondering if there were any other open source extensions that could allow this.

Just to be clear, I'm not talking about configuring SpringMVC to set up a RESTful JSON-based web service or anything like that, just if it's possible to do Spring app configuration via JSON instead of XML.

Answer

Tomasz Nurkiewicz picture Tomasz Nurkiewicz · Apr 19, 2012

As far as I know there is no project to support JSON as configuration source. It should be relatively easy to kick-start, (Spring container has no dependency on XML, it is just a way to construct bean definitions). However it is much more work than you might think.

Note that Spring provides to assist you in writing correct XML. You won't get that much in JSON. Also many DSLs were built on top of Spring XML and custom namespaces support (, and others use it).

If you hate XML (many do), try out Java Configuration, available since 3.0 and improved in 3.1:

@Configuration
public class MyBeans {

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

    @Bean
    public Bar bar() {
        return new Bar(foo());
    }

    @Bean
    public Buzz buzz() {
        Buzz buzz = new Buzz();
        buzz.setFoo(foo());
        return buzz;
    }

}

Interesting fact: thanks to some fancy proxying, foo() is called exactly once here, even though referenced twice.