Categories
java spring-boot

read value from application.properties spring boot

In this post, we will learn to read value from application.properties spring boot

In spring boot application we can easily inject the properties value from application.properties or application.yml

Using @value annotation we can inject the properties value from application.properties inside the java code

@value Syntax
@Value("${application.username}")
private String userName;

application.properties
#userName property
application.username=admin

# Add database url here
spring.datasource.url=jdbc:mysql://localhost:3306/beginnersbug
spring.datasource.username=root
spring.datasource.password=password
# Add driver class here 
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

In the below example, we are writing a rest service that will return the properties value from the application.properties

RestController

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class VariableInjectionController {

	@Value("${application.username}")
	private String userName;

	@GetMapping
	public String getUserName() {
		return userName;
	}
}

In the above example, we will return the application. username property via rest service

Default Value

In case of application. username property is not available means, we can set the default value for that property

Default Value Syntax
@Value("${application.name:BegineersBug}")
private String userName;

From the above snippet, it will take userName value as BeginnersBug because we don’t have application.name property in our application.properties

Github Link

https://github.com/rkumar9090/student-service/blob/master/src/main/java/com/beginnersbug/studentservice/controller/VariableInjectionController.java

Related Articles

configure datasource programmatically in spring boot

Categories
java properties

how to write properties file in java

In this tutorial, we will learn how to write properties file in java

we are going to use FileOutputStream to write in file. It is going to write on classpath

Syntax
properties.store(new FileOutputStream("database.properties"), "Comments: Updated from java");
Example Create new property file
import java.io.FileOutputStream;
import java.util.Properties;

public class WriteProperties {

	public static void main(String[] args) {
		try {

			Properties properties = new Properties();
			properties.setProperty("database.url", "jdbc:mysql://localhost:3306/beginnersbug");
			properties.setProperty("database.username", "root");
			properties.setProperty("database.password", "password");
			properties.store(new FileOutputStream("database.properties"), "Comments: Updated from java");
			System.out.println("File has writen successfully");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
Output
File has writen successfully
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/WriteProperties.java

Related Articles

how to load property file in java

 

Categories
java properties

how to load property file in java

In this post, we will learn how to load property file in java. Properties file is used to store and retrieve values from file.

It is stored as key and value pair. Below is an sample property file

database.properties
database.url=jdbc:mysql://localhost:3306/beginnersbug
database.username=root
database.password=password
Read properties from absolute path
import java.io.FileReader;
import java.util.Properties;

public class LoadProperty {

	public static void main(String[] args) {
		try {
			FileReader reader = new FileReader("D:\\BB\\Workspace\\database.properties");

			Properties p = new Properties();
			p.load(reader);

			System.out.println(p.getProperty("database.url"));
			System.out.println(p.getProperty("database.username"));
			System.out.println(p.getProperty("database.password"));

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
Output
jdbc:mysql://localhost:3306/beginnersbug
root
password
Read properties from class path

Please make sure you have database.properties in class path. else you will get Please check the file present in classpath

Syntax
LoadPropertyFromClassPath.class.getResourceAsStream("database.properties");

import java.io.InputStream;
import java.util.Properties;

public class LoadPropertyFromClassPath {

	public static void main(String[] args) {
		try {
			Properties prop = new Properties();
			InputStream stream = LoadPropertyFromClassPath.class.getResourceAsStream("database.properties");
			if (stream != null) {
				prop.load(stream);
				System.out.println(prop.getProperty("database.url"));
				System.out.println(prop.getProperty("database.username"));
				System.out.println(prop.getProperty("database.password"));
			} else {
				System.err.println("Please check the file present in classpath");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
Output
jdbc:mysql://localhost:3306/beginnersbug
root
password
Print all the values from property file
Syntax
prop.keySet().stream().map(key -> key + ": " + prop.getProperty(key.toString()))
						.forEach(System.out::println);
Example
import java.io.InputStream;
import java.util.Properties;

public class PrintPropertis {

	public static void main(String[] args) {
		try {
			Properties prop = new Properties();
			InputStream stream = LoadPropertyFromClassPath.class.getResourceAsStream("database.properties");
			if (stream != null) {
				prop.load(stream);
				prop.keySet().stream().map(key -> key + ": " + prop.getProperty(key.toString()))
						.forEach(System.out::println);
			} else {
				System.err.println("Please check the file present in classpath");
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
Output
database.password: password
database.url: jdbc:mysql://localhost:3306/beginnersbug
database.username: root
Github

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/LoadProperty.java

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/LoadPropertyFromClassPath.java

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/PrintPropertis.java

Related Articles

how to read a file from java with example

delete a file from java code