I am using Spring MVC. I have a UserService
class annotated with @Service
that has a lot of static variables. I would like to instantiate them with values from the application.properties file.
For example in application.properties I have: SVN_URL = http://some.url/repositories
Then in the class there is: @Value("${SVN_URL}") private static String SVN_URL
I get the Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
I have also tried @Autowired private static Environment env;
And then: private static String SVN_URL=env.getProperty("SVN_URL");
It gives the same error.
Advertisement
Answer
Think about your problem for a second. You don’t have to keep any properties from application.properties
in static fields. The “workaround” suggested by Patrick is very dirty:
- you have no idea when this static field is modified
- you don’t know which thread modifies it’s value
- any thread at any time can change value of this static field and you are screwed
- initializing private static field that way has no sense to me
Keep in mind that when you have bean controlled by @Service
annotation you delegate its creation to Spring container. Spring controls this bean lifecycle by creating only one bean that is shared across the whole application (of course you can change this behavior, but I refer to a default one here). In this case any static field has no sense – Spring makes sure that there is only one instance of UserService
. And you get the error you have described, because static fields initialization happens many processor-cycles before Spring containers starts up. Here you can find more about when static fields are initialized.
Suggestion
It would be much better to do something like this:
@Service public class UserService { private final String svnUrl; @Autowired public UserService(@Value("${SVN_URL}") String svnUrl) { this.svnUrl = svnUrl; } }
This approach is better for a few reasons:
- constructor injection describes directly what values are needed to initialize the object
final
field means that this value wont be changed after it gets initialized in a constructor call (you are thread safe)
Using @ConfigurationProperties
There is also another way to load multiple properties to a single class. It requires using prefix for all values you want to load to your configuration class. Consider following example:
@ConfigurationProperties(prefix = "test") public class TestProperties { private String svnUrl; private int somePort; // ... getters and setters }
Spring will handle TestProperties
class initialization (it will create a testProperties
bean) and you can inject this object to any other bean initialized by Spring container. And here is what exemplary application.properties
file look like:
test.svnUrl=https://svn.localhost.com/repo/ test.somePort=8080
Baeldung created a great post on this subject on his blog, I recommend reading it for more information.
Alternative solution
If you need somehow to use values in static context it’s better to define some public class with public static final
fields inside – those values will be instantiated when classloader loads this class and they wont be modified during application lifetime. The only problem is that you won’t be able to load these values from Spring’s application.properties
file, you will have to maintain them directly in the code (or you could implement some class that loads values for these constants from properties file, but this sounds so verbose to the problem you are trying to solve).