1. Application Properties & YAML configuration
2. YAML Configuration file.
3.
4. DemoApplication Class
package com.example.externalconfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
pageController:
msg: "Hello From YML config"
2. YAML Configuration file.
3.
4. DemoApplication Class
package com.example.externalconfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
5. PageController Class
package com.example.externalconfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PageController {
@Value("${pageController.msg}")
private String pageControllerMsg;
@RequestMapping("/")
public String home(){
return pageControllerMsg;
}
}
6. application.properties
server.port=9009
#pageController.msg=Hello from application.properties
Run the programmer.
7. Application.yml
msg: "Hello From YML config"
Run the programmer.
8. Changing from Command line arguments
Run the programmer.
10. Using properties value in application.properties
my.secret=${random.value}
my.number=${random.int}
my.bignumber=${random.long}
my.number.less.than.ten=${random.int(10)}
my.number.in.range=${random.int[1024,65536]}
11. Modify PageController
package com.example.externalconfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class PageController {
@Value("${pageController.msg}")
private String pageControllerMsg;
@Value("${my.secret}")
private String mysecret;
@RequestMapping("/")
public String home(){
return mysecret;
}
}
Run the program.


Comments
Post a Comment