Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use properties from a configuration (properties/yml) file in my Spring Boot application?

how can I use external configurations within my Spring application?

package hello.example2.Container

import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.client.RestTemplate

@RestController
class ContainerController {
    @RequestMapping("/container/{cid}")
    public list(@PathVariable Integer cid) {
        def template = new RestTemplate();
        def container = template.getForObject("http://localhost:5050/container/" + cid.toString(), Container);
        return container;
    }
}

I want to replace "http://localhost:5050" with a configuration option (f.e. application.yml or application.properties).

This is my application file (Groovy):

package hello.example2

import groovy.transform.CompileStatic
import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.context.annotation.Configuration

@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@CompileStatic
class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

I tried to set "@Configuration" and "@EnableAutoConfiguration" but to be honest I don't know what they are doing. I'm new to Java/Groovy and the Spring framework (but not to programming in general).

I have read these pages but there is no complete example only snippets:

[1] http://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html

[2] https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

like image 914
MonkeyMonkey Avatar asked Aug 26 '16 11:08

MonkeyMonkey


2 Answers

In your configuration file (application.yml or application.properties) add a new entry:

endpointUrl: http://localhost:5050

Then inject that property in your controller:

@RestController
class ContainerController {

    @Value("${endpointUrl}")
    private String ednpointUrl;

    @RequestMapping("/container/{cid}")
    public list(@PathVariable Integer cid) {
        def template = new RestTemplate();
        def container = template.getForObject(endpointUrl+"/container/" + cid.toString(), Container);
        return container;
    }
}
like image 115
codependent Avatar answered Sep 22 '22 12:09

codependent


Though above answers your question. I'm not that convinced with that. Let me elaborate one by one:

@Configuration: You could have spring configuration set in two ways. a) Using xml file. b) And using Java Configuration class.

To use Java class as configuration file for spring, you need to add @Configuration annotation. In this Configuration class, now you could specify beans using @Bean tag. Specify scope as singleton or prototype using @Scope annotation. And everything you can do using xml configuration file.

Also, a wide community don't like xmls to be used for configurations and hence, they prefer configuring spring in java classes.

@EnableAutoConfiguration: is from springboot framework. It automatically configures conventional or expected configurations which could later be overridden. Basically it wisely configures default beans required by default using different java Configuration classes for different default configurations.

Regarding externalising configuration: You could externalise configuration to application.properties or application.yml. Following should be the location.

  1. A /config subdirectory of the current directory.
  2. The current directory
  3. A classpath /config package
  4. The classpath root

The above are ordered in sequence of their precedence. Hence if config directory is present under current project directory. It would have highest precedence.

Below I am adding a tree diagram of my application. May be this helps.

vinayprajapati@localhost:~/Desktop/project/gradleSpringBoot$ tree
.
├── build.gradle
├── gradleSpringBoot.iml
└── src
    ├── main
    │   ├── groovy
    │   │   └── org.test
    │   │       ├── components
    │   │       │   └── TestComponent.groovy
    │   │       ├── configuration
    │   │       │   └── BaseConfiguration.groovy
    │   │       |
    │   │       └── utils
    │   │           ├── AppConfig.groovy
    │   │           └── AppInfo.groovy
    │   ├── java
    │   │   └── org
    │   │       └── test
    │   │           ├── GradleSpringBootApplication.java
    │   │           └── HelloController.java
    │   └── resources
    │       ├── application-qa.yml
    │       ├── application-uat.yml
    │       ├── application.properties
    │       ├── application.yml
    │       ├── static
    │       └── templates
    └── test
        └── java
            └── org
                └── test
                    └── GradleSpringBootApplicationTests.java

Looks like you are having troubleshooting problem.

Let's do one thing. Put the config file in the resource like in tree diagram above and delete same from all the other places in project. Reason for doing this is that any config file in the root directory or /config subdirectory has higher precedence and hence would override config which is in classpath.

like image 32
Vinay Prajapati Avatar answered Sep 21 '22 12:09

Vinay Prajapati