Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Spring Support JSON Configuration?

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.

like image 911
donalbain Avatar asked Apr 19 '12 18:04

donalbain


People also ask

Can Spring be configured using JSON?

The Spring Boot framework provides a simple approach to load external JSON data through the command line. In case of need, we can load JSON data through properly configured PropertySourceFactory. Although, loading nested properties is solvable but requires extra care. As always, the code is available over on GitHub.

What is the configuration file for Spring?

A Spring configuration file is an XML file that contains the classes information. It describes how those classes are configured as well as introduced to each other. The XML configuration files, however, are verbose and cleaner.

Does spring boot use JSON?

Spring Boot provides integration with three JSON mapping libraries: Gson.

How many types of configuration are there in Spring?

There are either only two ways to configure the Spring framework. The two basic configuration tools for the Spring framework are: XML files (outside java files) Java (5 +) based annotations (inside java files)


2 Answers

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 xml-schema 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 (spring-integration, mule-esb 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.

like image 133
Tomasz Nurkiewicz Avatar answered Oct 11 '22 21:10

Tomasz Nurkiewicz


Try JSConf library available on maven central, it's support Properties, HOCON and JSON format.

You can inject values from external file to your service and more !

Sample usage of JavaConfig :

You data stored on file app.conf

{ 
"root":{
"simpleConf":{
    "url":"Hello World",
    "port":12,
    "aMap":{
        "key1":"value1",
        "key2":"value2"
    },
    "aList":[
        "value1",
        "value2"
    ]
}}

You service where your configuration must be inject

@Service("service")
public class Service {
    @Autowired
    private ConfigBean configBean;
}

Declare a interface to access your configuration values from your service

@ConfigurationProperties("root/simpleConf")
public interface ConfigBean {
    String getUrl();
    int getPort();
    Map getAMap();
    List getAList();
}

And your Spring configuration bean :

@Configuration
public class ContextConfiguration {
    @Bean
    public static ConfigurationFactory configurationFactory() {
        return new ConfigurationFactory().withResourceName("app.conf") //
                .withScanPackage("org.jsconf.core.sample.bean");
    }
}
like image 34
Yves Galante Avatar answered Oct 11 '22 21:10

Yves Galante