You are on page 1of 3

-by RAGHU SIR, NARESH IT, AMEERPET, HYD.

Spring Boot Input Files


https://www.facebook.com/groups/thejavatemple/

=> If we are using Spring f/w, then Programmer has to


a. Define Spring Configuration code (XML/Java..)
b. Programmer has to give inputs to config code

=> Coming to Spring boot, it takes care of Define Spring Configuration code
(AutoConfiguration)
But here, programmer you need to provide inputs

=> Inputs?? 2 files (key=val)


a. application.properties
b. application.yml

-----------------------------------------------
https://docs.spring.io/spring-boot/docs/current/reference/html
/appendix-application-properties.html#transaction-properties

--------------------------------------------------------------
** To load properties file into Spring container code in Spring f/w
@PropertySource("classpath:application.properties").
In Spring boot also same code but written by boot only.

---------------------------------------------------------------
Note:
a. all keys must be placed inside application.properties which is under
src/main/resources(classpath)
b. key is String type, value can be any type like int/double/string/boolean..etc
c. Properties file is loaded by using code like:
@PropertySource("classpath:application.properties") by Spring boot only.

d. Spring container creates object to Environment(I) where Impl class is


StandardEnvironment(C) for normal programming. Unit Testing object is
MockEnvironment(C).

e. To read these keys in our application, we use code types


i. @Value
1|Page
-by RAGHU SIR, NARESH IT, AMEERPET, HYD.

ii. Environment object autowired


iii. *** Configuration Properties(bulk load)

------code-----------------------
application.properties
#Hello from properties
app.title=SAMPLE ONE
app.version=1.0

2|Page
-by RAGHU SIR, NARESH IT, AMEERPET, HYD.

app.active=true

Runner class code:


package in.nit.runner;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class InputReadRunner
implements CommandLineRunner
{

//@Value("${key}")
@Value("${app.title}")
private String ttle;
@Value("${app.version}")
private double ver;
@Value("${app.active}")
private boolean active;

@Override
public void run(String... args) throws Exception {
System.out.println("from runner: " + ttle +"-
"+ver+"-"+active);
}
}

3|Page

You might also like