I'm trying to read the values in the property file. Why I get values: [] in the output?
abc.env[0].envname=test
abc.env[0].grant_type=password
abc.env[1].envname=dev
abc.env[1].grant_type=password
Below is the class I'm trying to execute
@SpringBootApplication
public class AbcApplication implements CommandLineRunner{
private static Logger LOG = LoggerFactory.getLogger(AbcApplication.class);
@Autowired
ReadAbcApplicationProperties readAbcApplicationProperties;
public static void main(String[] args) {
SpringApplication.run(AbcApplication.class, args);
}
@Override
public void run(String... args) {
LOG.info("values: {}", readAbcApplicationProperties.getAbcSources());
}
}
@Component
@ConfigurationProperties(prefix = "abc")
public class ReadAbcApplicationProperties {
private List<AbcProperties> abcSources = new ArrayList<AbcProperties>();
@Autowired
ReadAbcApplicationProperties readAbcApplicationProperties;
public List<AbcProperties> getAbcSources() {
return abcSources;
}
public void setAbcSources(List<AbcProperties> abcSources) {
this.abcSources = abcSources;
}
}
public class AbcProperties {
private String envname;
private String tokenGrantType;
public String getEnvname() {
return envname;
}
public void setEnvname(String envname) {
this.envname = envname;
}
public String getTokenGrantType() {
return tokenGrantType;
}
}
Can someone help me with the missing part?
CodePudding user response:
There are some inconsistencies in your code..
ReadAbcApplicationProperties
- Why you inject itself in ?@Autowired ReadAbcApplicationProperties readAbcApplicationProperties;
This line must be removed.
field
private String tokenGrantType
is not consistent withabc.env[0].grant_type
in properties fileprivate List<AbcProperties> abcSources
is not consistent withabc.env[0]
andabc.env[1]
and ...abc.env[X]
in properties file
The classes should be:
AbcProperties
public class AbcProperties {
private String envname;
private String grantType; //to match `grant_type` in .properties file
//getters setters
}
ReadAbcApplicationProperties
@ConfigurationProperties(prefix = "abc")
public class ReadAbcApplicationProperties {
private List<AbcProperties> env = new ArrayList<>(); //to match abc.env[X] in properties file
// getters setters
}
I tried the above in a spring-boot sample project and it works
CodePudding user response:
You have different property names in property file and corresponding bean class AbcProperties
. see tokenGrantType
and grant_type
.
An ideal configuration would be:
Apologies for using yml file instead of properties, but it all behaves the same. It's just yml
would gives us more readability.
myapp: # this can be your prefix
abc:
-
envname: test
grant_type: password
-
envname: dev
grant_type: password
Having "myapp" as prefix(top-level key), woudl enable you to add any number of properties inside a single class like below 'ConfigPropertiesBinder'.
@Data // If not, include getter and setters, with cons.
@RefreshScope
@Configuration
@ConfigurationProperties(prefix = "myapp")
public class ConfigPropertiesBinder {
private List<AbcProperties> abc; // Should match with myapp.abc
// Other properties
}
@Data
public class AbcProperties{
private String envname; // Match with myapp.abc[x].envname
private String grantType; // '-' and '_' converts to camelcase
}
See if it helps.