Reputation: 699
I am using spring boot and I need to get the instance of the application.properties file loaded at startup; is it possible?? (a possible workaround could be to find the file path of application.properties loaded at startup...but how can get it?)
currently I have an application.properies into my project but in future i have to get it from an environment variable that will contains the full path
the class that contains the management of my properties is like that:
@Component
public class SpringBootConfigurations {
public static String PROP_JIRA_USER;
@Value("${jira.user}")
private void setJiraUser(String user) {
PROP_JIRA_USER = user;
}
public static String PROP_JIRA_PASSWORD;
@Value("${jira.password}")
private void setJiraPassword(String password) {
PROP_JIRA_PASSWORD = password;
} etcc...
thanks
Upvotes: 0
Views: 2155
Reputation: 2891
It sounds like you need the properties as a java File object to match the signature of your SMTP library. One way to do that is to just read the properties as a classpath resource.
@Value("classpath:data/application.properites")
Resource resourceFile;
From there you can convert to a file with a getFile
call and pass it to the library.
However, you may be better off creating a ConfigurationProperties
object and just generating a file programmatically with a FileWriter and passing that along. It would be more declarative and clearer in the implementation exactly what is happening.
To add a bit more detail on Configuration properties. You could do something like this:
@Configuration
@ConfigurationProperties(prefix = "smtp")
public class ConfigProperties {
private String host;
private int port;
private String username;
// other smtp props
// standard getters and setters
public File propsAsFile() {
Properties props = new Properties();
props.setProperty("smtp.host", host);
props.setProperty("smtp.port", port);
...
File temp = File.createTempFile("xyz",".properties");
FileOutputStream propsStream = new FileOutputStream(temp)
prop.store(propsStream, null);
}
}
Also @Kayaman is correct in the comments. Only go through these file gymnastics if you absolutely need it (seems unlikely that you need all of this but depends on the library you're using)
For more on ConfigurationProperties https://www.baeldung.com/configuration-properties-in-spring-boot
Upvotes: 2
Reputation: 24444
Spring (Boot) offers a very comprehensive and flexible configuration mechanism that abstracts from the source of configuration. This mechanism is based on a well defined property override order that is specified in Chapter 2: Externalized Configuration.
Considering this documentation, there are several ways how you could get your configuration parameters into your Spring Boot application (non-exhaustive):
--smpt.port=1234
)-Dsmpt.port=1234
)@PropertySource
annotation on your @Configuration
classes. As the @PropertySource
value can contain ${...}
placeholders, it should be possible to use an environment variable here to refer to your properties file.PropertySource
-implementation. (Although I think that's not necessary for your purposes.)See also https://www.baeldung.com/properties-with-spring.
Upvotes: 0