Read Static Varibles From Application.properties Spring Boot

Spring Kicking - Application Properties


Application Properties support us to work in dissimilar environments. In this chapter, yous are going to acquire how to configure and specify the backdrop to a Bound Boot application.

Control Line Properties

Bound Boot application converts the control line properties into Bound Boot Environs properties. Command line properties have precedence over the other belongings sources. By default, Spring Boot uses the 8080 port number to first the Tomcat. Permit usa larn how modify the port number past using command line properties.

Stride one − Subsequently creating an executable JAR file, run it by using the command java –jar <JARFILE>.

Step 2 − Apply the control given in the screenshot given below to alter the port number for Spring Boot application by using control line properties.

Command Line Properties JARFILE

Notation − You can provide more than one application properties past using the delimiter −.

Properties File

Properties files are used to go along 'North' number of properties in a single file to run the awarding in a different environment. In Spring Boot, properties are kept in the application.properties file nether the classpath.

The application.backdrop file is located in the src/main/resources directory. The code for sample application.backdrop file is given beneath −

server.port = 9090 spring.application.name = demoservice        

Note that in the code shown above the Spring Boot application demoservice starts on the port 9090.

YAML File

Bound Boot supports YAML based properties configurations to run the awarding. Instead of application.properties, we tin can use awarding.yml file. This YAML file also should be kept inside the classpath. The sample awarding.yml file is given beneath −

spring:    application:       name: demoservice    server: port: 9090        

Externalized Properties

Instead of keeping the properties file under classpath, we can go along the properties in unlike location or path. While running the JAR file, nosotros can specify the properties file path. Y'all can utilize the following control to specify the location of backdrop file while running the JAR −

-Dspring.config.location = C:\application.properties        

Externalized Properties

Employ of @Value Notation

The @Value annotation is used to read the environment or awarding belongings value in Coffee code. The syntax to read the property value is shown below −

@Value("${property_key_name}")        

Look at the post-obit case that shows the syntax to read the spring.application.name belongings value in Java variable by using @Value notation.

@Value("${spring.application.name}")        

Observe the code given beneath for a improve understanding −

import org.springframework.beans.manufacturing plant.notation.Value; import org.springframework.kick.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.demark.annotation.RestController;  @SpringBootApplication @RestController public grade DemoApplication {    @Value("${leap.awarding.proper name}")    private String proper name;    public static void main(Cord[] args) {       SpringApplication.run(DemoApplication.form, args);    }    @RequestMapping(value = "/")    public String proper name() {       render name;    } }        

Annotation − If the property is not found while running the application, Spring Kicking throws the Illegal Argument exception as Could not resolve placeholder 'spring.awarding.name' in value "${spring.application.proper name}".

To resolve the placeholder upshot, we tin set up the default value for the property using thr syntax given beneath −

@Value("${property_key_name:default_value}")  @Value("${jump.awarding.proper name:demoservice}")        

Spring Kick Active Profile

Bound Boot supports unlike properties based on the Jump active profile. For example, we tin can keep two split up files for evolution and production to run the Leap Kicking application.

Leap active contour in application.properties

Let us understand how to have Jump active profile in application.properties. By default, application. properties will be used to run the Spring Boot application. If yous desire to use profile based properties, we can keep carve up backdrop file for each profile every bit shown below −

application.properties

server.port = 8080 spring.application.name = demoservice        

application-dev.properties

server.port = 9090 spring.application.name = demoservice        

application-prod.backdrop

server.port = 4431 spring.awarding.proper noun = demoservice        

While running the JAR file, we need to specify the bound agile profile based on each properties file. By default, Spring Boot application uses the awarding.properties file. The control to set the spring active profile is shown below −

Prod.Properties Active Dev

You can come across active profile name on the console log as shown below −

2017-eleven-26 08:13:16.322  INFO 14028 --- [               primary] com.tutorialspoint.demo.DemoApplication  :    The following profiles are active: dev        

Now, Tomcat has started on the port 9090 (http) as shown below −

2017-11-26 08:xiii:20.185  INFO 14028 --- [               chief] due south.b.c.e.t.TomcatEmbeddedServletContainer :     Tomcat started on port(s): 9090 (http)        

Y'all can ready the Production active profile as shown below −

Production Active Profile

You tin see active profile name on the console log equally shown below −

2017-11-26 08:xiii:16.322  INFO 14028 --- [               main] com.tutorialspoint.demo.DemoApplication  :    The post-obit profiles are agile: prod        

Now, Tomcat started on the port 4431 (http) as shown below −

2017-11-26 08:thirteen:20.185  INFO 14028 --- [              principal] s.b.c.e.t.TomcatEmbeddedServletContainer :    Tomcat started on port(southward): 4431 (http)        

Bound agile contour for application.yml

Let u.s. sympathize how to keep Spring active profile for application.yml. We can go along the Jump active contour properties in the single application.yml file. No need to use the separate file similar application.backdrop.

The post-obit is an instance code to keep the Spring active profiles in awarding.yml file. Notation that the delimiter (---) is used to separate each contour in awarding.yml file.

spring:    application:       name: demoservice server:    port: 8080  --- leap:    profiles: dev    awarding:       proper noun: demoservice server:    port: 9090  --- spring:     profiles: prod    awarding:       name: demoservice server:     port: 4431        

To command to set development agile profile is given below −

Prod.Properties Active Dev

Y'all can run into active profile name on the console log as shown beneath −

2017-11-26 08:41:37.202  INFO 14104 --- [               main] com.tutorialspoint.demo.DemoApplication  :     The following profiles are active: dev        

Now, Tomcat started on the port 9090 (http) equally shown beneath −

2017-eleven-26 08:41:46.650  INFO 14104 --- [               main] due south.b.c.east.t.TomcatEmbeddedServletContainer :     Tomcat started on port(s): 9090 (http)        

The command to fix Production agile profile is given below −

Production Active Profile

You tin see active profile name on the console log every bit shown below −

2017-eleven-26 08:43:x.743  INFO 13400 --- [        chief] com.tutorialspoint.demo.DemoApplication  :     The following profiles are agile: prod        

This will start Tomcat on the port 4431 (http) as shown below:

2017-11-26 08:43:xiv.473  INFO 13400 --- [         primary] south.b.c.e.t.TomcatEmbeddedServletContainer :     Tomcat started on port(south): 4431 (http)        

Useful Video Courses


Mastering Spring Framework

Video

Spring Boot: A quick tutorial guide

Video

Vue JS + Spring Boot Microservices and Spring Cloud

Video

Step By Step React Native Hybrid Mobile App With Spring Boot

Video

React Full Stack Web Development With Spring Boot

Video

Angular 8 Full Stack Development With Spring Boot

Video

spradlinlonty1949.blogspot.com

Source: https://www.tutorialspoint.com/spring_boot/spring_boot_application_properties.htm

0 Response to "Read Static Varibles From Application.properties Spring Boot"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel