Spring-Boot: How to load a keystore resource file either from the classpath or from the file system?

My spring-boot application requires access to private keys via a Java keystore. For testing, I would like to use a test keystore that is available on the classpath (so I can easily execute the application as part of an integration test), while in production I would like to use a keystore from some external volume (e.g., mounted inside a container as k8 secret).

I know that the spring-boot @Value annotation allows me to inject a resource. However, I don’t see how it can be made dynamic to respect the spring environment (e.g., test or production). Does spring-boot provide an out-of-the-box way to solve the above problem, or do I need to write my own bean to load the correct keystore depending on the deployment environment?

>Solution :

You can use a property for the resource path and set that property values in your application props differently based on an active profile.

private Resource myResource;


            on-profile: test
my.resource.path: 'classpath:test/path/keystore.jks'

            on-profile: prod
my.resource.path: 'prod/path/keystore.jks'

Leave a Reply