springboot how to load properties from custom yml - java

I have a property bean (example):
#Data
public class MyProperty {
private String name;
private String address;
}
and I have a custom yml file in classpath named: my_property.yml
my.property:
name: testName
address: testAddress
How to load this file to my property bean?
Not using #PropertyResource, because I want to use the yml file.
Thanks.

#PropertySource is the way to include external/custom properties but unfortunately is doesn't work with yaml files due to this issue.
You need to load these yaml files on your own by simply writing EnvironmentPostProcessor and adding it META-INF/spring.factories.
This is described in below Spring documentation (slight difference among versions).
Spring boot 1.5.x
Spring boot 2.x

There are two properties you can look at -
spring.config.name - If you don't like application as your file name
for eg-
java -Dspring.config.name = my_property myjar.jar
spring.config.location - To tell where your files lie
More information is well-documented here
spring documentation

Follow: http://www.baeldung.com/spring-yaml
The relative path of application.yml file is /myApplication/src/main/resources/application.yml.
and access is:
#Autowired
private YAMLConfig myConfig;
I put my application.properties file at src/main/resources/
End my #SpringBootApplication read.
application.properties file
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=***
spring.datasource.username=***
spring.datasource.password=***

Related

Binding application.properties to a class from external library in Spring

Let's say I have an external jar (that supposed to work in spring boot env) that has this simple class:
#Component
#ConfigurationProperties("test")
public class NetworkConfig {
//getters/setters
...
}
Now I use this jar as dep in a Spring project (NOT Spring Boot!!).
I have an application.properties file in that project and want to load properties from it to this class and it should be available in a context. How would I do it?
I also need to mention that external jar is my lib and I can modify it if needed.
#ConfigurationPropertiesScan ("path_to_property")

Spring Boot Property file precedence

In my Spring Boot program I'm getting a failure due to a bad property value on load. In particular, it uses the DB2 hibernate dialect but it's not defined in the property file I thought I was using.
Assuming no annotations, where does Spring look for the properties file? Yes I know it normally resides in src/main/resources/application.properties
What if I have a property in my test cases; does it ignore the one in main and use the one in test? Or does it start with the main version and let the test one override the main where it applies?
Does the application profile affect the property file used? Some people use the same application.properties file name in both main and test.
If I do have a TestSource annotation with a class path location, does it still augment it with something somewhere else?
Finally, how can I get Spring to tell me everywhere it looked for properties and not just one of them?
#Woodsman
It's possible to use many settings in each profile/environment in spring application on src/main/resources/ folder
application-dev.properties
application-local.properties
application-onlytests.properties
application-prd.properties
application.properties
the name after hyphen will be the name profile got by spring
Ex: application-{myenviroment}.properties
When you start spring application will be used application.properties by default.
You can tell spring to use an specific properties passing the environment in one of ways below:
Putting spring.profiles.active=prd inside application.properties file
Passing by parameters when start spring app --spring.profiles.active=local
Running your jar on command line java -jar myjar.jar --spring.profiles.active=dev
Setting an environment var in your machine/docker/container SET SPRING_ACTIVES_PROFILE=local
There are other ways using annotations on beans, passing jvm arguments and others
If you need run your tests in a specific configuration ( vars, database, settings ), It's possible to pass which .properties will be used
#ExtendWith(SpringExtension.class)
#AutoConfigureMockMvc
#SpringBootTest
#TestPropertySource(locations = "classpath:application-onlytests.properties")
public class RunTest_from_onlytests_properties {
#Autowired
private MockMvc mockMvc;
#Test // org.junit.jupiter.api.Test
public void test() throws Exception{
// ...
}
}

Is there is a way to make a spring boot app to be able to see a config folder in Resources and get its config whenever it needs?

I have a config folder of xml files that my spring boot app needs , its located on the same level of src folder ... Now i now to locate it in Resources Folder on the same level of application.properties.. There is any way to be able to do that ?
There many way to read your file .properties:
You can read with plain java like this:
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName));
you can use the #PropertySource annotation in a Spring #Configuration class:
#Configuration
#PropertySource("db.properties")
public class ApplicationConfig {
// more config ...
}
//Or if you your config outside of your classpath, you can use the file: prefix:
#PropertySource("file:/path/to/application.properties")
Using Multiple file :
#PropertySources({
#PropertySource("classpath:foo.properties"),
#PropertySource("classpath:bar.properties")
})
public class PropertiesWithJavaConfig {
//...
}
And so on ...

#ConfigurationProperties usage in spring

I have a project built up on struts framework but I've integrated Spring Framework in same(using a servlet-config.xml) and using the MVC and AOP in it, now in one requirement I must use one project which is built up on SpringBoot and using #ConfigurationProperties binding an application.yml file with a POJO.
I just need to use the code of that project and hence added the dependency of that project. Is there any way I can use #ConfigurationProperties to bind the POJO with the yml file in my project. I tried but bean is not binding with the yml file entries.
As far as I know #ConfigurationPropertiesis part of SpringBoot features only.
Please give your useful suggestion.
You can do something like for example below:
if your yml file has properties containing same prefix then you could use as below:
POJO class:
#Configuration // add this if your spring boot version is < 2.2 or else please donot add.
#ConfigurationProperties(prefix = "spring.db.props") //example
public class MyPojoProps{
private String userName;
private int password;
private String uri;
// standard getters and setters
}
your application.yml may be something like below:
application.yml
spring:
db:
props:
userName:
password:
uri:
Please refer here for more information

External YAML property file and normal properties file in Spring Boot

This is how I have set up external configuration file:
/** for .yaml file
#Configuration
#PropertySource("classpath:waterlvl.yaml")
#ConfigurationProperties("Alerts")
public class WaterLvlAlertConfig {
/** for .properties file
#Component
#PropertySource("classpath:waterlvl.properties")
public class WaterLvlConfig {
Properties are not being picked up, and my files are inside the resources file. I think the path I'm providing is wrong. What am I doing wrong?
In spring boot #PropertySource cannot be used when using .yaml configuration. Refer to point 24.6.4 YAML shortcomings in externalized configuration documents. Also, a similar question is answered earlier, refer - Spring boot external configuration of property file

Categories