How to assign a value from application.properties to a static variable?

前端 未结 3 1373
执笔经年
执笔经年 2020-12-02 08:18

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

3条回答
  •  感动是毒
    2020-12-02 08:34

    Spring does not allow to inject value into static variables.

    A workaround is to create a non static setter to assign your value into the static variable:

    @Service
    public class UserService {
    
        private static String SVN_URL;
    
        @Value("${SVN_URL}")
        public void setSvnUrl(String svnUrl) {
            SVN_URL = svnUrl;
        }
    
    }
    

提交回复
热议问题