Read YAML file like properties - spring-boot

I am developing a spring boot project, and I need some external configuration. So I am storing that in a file called "config.yml" placed in "src/main/resources" folder.
Now I want the properties in config.yml to be injected in my class. Now if we use SnakeYML or any other parser, we would need to make Java classes to define the schema.
What I want is I can read the yml just like properties using #Value annotation. For e.g.
logging:
class:
name:
location:
I need to access "name" or "location" property using
#Value(${logging.class.name})
private String name;
Is there a way to do that in spring boot?

you don't need to add separate yml. you add custom properties to application.yml or application-{env}.yml
Spring recognise it and you can it via
#Value
Spring environment
Using Spring ConfigurationProperties

Related

How to access defined in the application.properties file in Spring Boot?

I want to access values provided in application.properties, e.g.:
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
logging.file=${HOME}/application.log
userBucket.path=${HOME}/bucket
I want to access userBucket.path in my main program in a Spring Boot application
You can use the #Value annotation and access the property in whichever Spring bean you're using
#Value("${userBucket.path}")
private String userBucketPath;

Load active properties file spring boot

I want to read values from an active profile or can say active properties file.
I have three properties files
application-dev.properties
application-stage.properties
application-prod.properties
I have set an active profile to dev as follows
spring.profiles.active=dev
My application-dev.properties file has one entry that i want to read in my class.
application-dev.properties file
fix.connection.type=initiator
I tried reading this entry
#Configuration
#PropertySource("classpath:application-${spring.profiles.active}.properties")
#Component
public class AdaptorDestination {
#Value("${fix.connection.type}")
private String connectionType;
}
Exception
nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.profiles.active' in value "classpath:application-${spring.profiles.active}.properties"
Please help me out
You don't need to use #PropertySource annotation in order to load a property using #Value annotation.
Spring will load #Value property automatically from the current active profile.
Also, you don't need to specify ${spring.profiles.active} in the #PropertySource annotation as Spring always loads properties from the currently active profile by default.
Spring automatically resolves file name based on the profile suffix.
So you need only specify the base filename, e.g.:
#PropertySource("classpath:application.properties")
If the currently active profile is "dev" Spring will load properties at first from application.properties file and then override them with properties from application-dev.properties file.
You can read more here
You can use the annotation #Profile("profile-name") to determinate when to execute the method.
Spring Profiles

Yaml type-safe configuration in Spring using Kotlin Functional Style

I'm trying to follow the demo https://github.com/sdeleuze/spring-kotlin-functional to create a new Spring Boot application using the annotation-free new approach, released in Spring Boot 2. My problem is how to continue to use Yaml files to configure my application, without using annotations? I would guess it would be something inside the Beans configuration but I dont find any documentation on that subject. Thanx
The beans dsl has an env property that you can use to retrieve any environment property defined in yaml, properties files or command line parameters:
fun beans() = beans {
bean<SomeBeanThatNeedsConfig> {
SomeBeanThatNeedsConfig(env.getProperty("config.value"))
}
}

How to add properties programmatically(just like adding key-value into application.properties)?

I have some common properties that every projects should set, such as
feign.hystrix.enabled=false
feign.httpclient.enabled=true
I don't want to repeatedly add these props in every project so I'm going to create an extra jar file containing #Configruation class. How to add properties in #Configuration class? Thanks!
PropertySources
You may load an application.properties from another jar this way:
#PropertySources({
#PropertySource("classpath:common.properties")
})
#Configuration
public class SomeJavaConfig {
}
You can find the reference in Spring's documentation:
Spring Boot uses a very particular PropertySource order that is
designed to allow sensible overriding of values. Properties are
considered in the following order:
...
#PropertySource annotations on your #Configuration classes.
Spring-cloud-config
I won't go in all the details, but another option is to use spring-cloud-config to define these properties in a git (using spring-cloud-config-server). Then, have your spring-boot application load the application.properties using spring-cloud-config-client directly from git.
Check this:
https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html
https://spring.io/guides/gs/centralized-configuration/

spring boot setting up message.properties and errors.properties file in the project structure and reading file to code

I am new to spring boot. I want to add external properties files in project structure . Files are errors.properties, messages.propeties and sql.properties file which contains the all sql queries. I get it where to add it i.e \demo\src\main\resources\errors.properties file. Can Anyone of you give me insight how to read from these files to my java code .
The easiest way would be to leverage what Spring Boot already give you automatically. Anything you put into application.properties (under \demo\src\main\resources) is going to be added to your Environment. I would just take the keys from those three files and create unique entries in application.properites
errors.key1=value1
errors.key2=value2
sql.key1=value1
....
Then you can use the #ConfigurationProperties annotation to map those configurations to a class that encapsulates each type.
#Component
#ConfigurationProperties(prefix="errors")
public class ErrorSettings {
private String key1;
.....
//getter and setters
}
Now you have a Bean of type ErrorSettings that you can inject into any other Bean you declare and just call the getXXX() method of the configuration you want.
Reference doc:
http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html

Resources