Skip to content
Advertisement

How to set up init method with file constants for Spring Application?

I’m pretty new in Java Spring usage, so here’s my question. I need to inject some information into DB before my application starts. I mean, I know how to use @Bean init-method, but i dont’t want to hardcode constants even in .properties file. Here’s my temporary solution (data is changed):

JavaScript

But this IS hardcode, what’s more, every client will have its own list of constants. Can You help me with such a solution, where it’s enough to pass a file name as a .jar starter parameter/Path variable and generate defaults from that file. For example:

JavaScript

Advertisement

Answer

The answer is rather obvious – annotation @Value("${your_arg}") automatically checks your application.properties file and if there’s no such property tries to find it in command line arguments (all this actions are done before context starts). That’s why you can simply make something like this:

JavaScript

After that, you can simply build your jar file and start it with:

JavaScript

It works fine with JpaRepositories and DB seeding, but I haven’t checked it with application.properties overriding.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement