For some reason non-nested properties load but nested don't.
Configuration:
spring:
profile: junit
profiles:
include: base
Config class:
@ConfigurationProperties(prefix = "spring")
public class MyFirstProperties {
private String profile;
private Profiles profiles;
// getters and setters
public class Profiles
{
private String include;
// getters and setters
}
}
Main class:
@SpringBootApplication
@EnableConfigurationProperties(MyFirstProperties.class)
public class Main {
public static void main(String... args) {
SpringApplication.run(Main.class, args);
}
}
When I inject configuration class to my controller and call getter for a non-nested property it returns its value. But a getter for a nested property returns null.
Annotating inner class with ConfigurationProperties and its own prefix does not seem to work. Am I missing something?
To access the YAML properties, we'll create an object of the YAMLConfig class, and access the properties using that object. In the properties file, we'll set the spring. profiles. active environment variable to prod.
Another method to access values defined in Spring Boot is by autowiring the Environment object and calling the getProperty() method to access the value of a property file.
We can simply define an application-environment. properties file in the src/main/resources directory, and then set a Spring profile with the same environment name. For example, if we define a “staging” environment, that means we'll have to define a staging profile and then application-staging. properties.
You need to instantiate your profiles
property
private Profiles profiles = new Profiles();
That's it.
This happens because your inner class
isn't static
.
You cannot instantiate this type of class
directly, but only inside the context of the enclosing one.
Make your class
static
and you'll be good to go
public static class Profiles { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With