Reputation: 3984
I want to access my application.properties file from a jar I created and included in my pom.
So in parent project -
<dependency>
<groupId>some.group.name</groupId>
<artifactId>some-jar-name</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
And inside the jar I have a class that needs values from the parent application.properties
file.
I already tried using spring.profiles.active
by setting spring.profiles.active=test
and printing JVM args but the values was not injected.
Inside the jar -
public class PropertiesRepository {
//get application.properties
public PropertiesRepository(String appName, String instance) {
RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
List<String> arguments = runtimeMxBean.getInputArguments();
for (Iterator iterator = arguments.iterator(); iterator.hasNext();) {
String param = (String) iterator.next();
System.out.println("Runtime arg=" + param);
}
}
}
Upvotes: 1
Views: 1977
Reputation: 21134
As your file will be on the classpath, use a @Configuration
class with @PropertySource
.
@Configuration
@PropertySource("classpath:your-file-name.properties")
public class PropertiesConfiguration { }
If your file name is the standard application.properties
, you don't even need to do that, it will be picked up automatically, and you'll be able to access them via
@Value("${your-property}")
or
Environment#getProperty("your-property")
Given you're on Spring Boot
, everything should work flawlessly with your current dependency configuration.
Upvotes: 1