Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring - read property value from properties file in static field of class

I have one utility class where i have one method which requires username and password to connect other url. I need to kept that username in properties file so that i can change it any time. But as i am using it in static method (being utility class) , Issue is it is showing null .(i.e. it is not able to read from properties file).

But when i ckecked that values in some other controller they are getting there. So my question is how to read property value in static field

<bean
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath*:/myservice_detaults.properties</value>
            <value>classpath*:/log4j.properties</value>
        </list>
    </property>
</bean>

//in Utitlity class code

  @Value("${app.username}")
      static String userName;

public static connectToUrl(){
  //use userName
 //userName showing null
}
like image 514
user3608352 Avatar asked Jul 09 '14 08:07

user3608352


1 Answers

Try this : Make your class a Component

@Component
public class UserXXXUtils {
    private static Integer trustXXXMask;

    @Value("${trustXXXMask}")
    public void setTrustXXXMask(Integer trustXXXMask) {
        UserXXXUtils.trustXXXMask = trustXXXMask;
    }
    //Access anywhere in the class
}
like image 144
Manas Ranjan Mahapatra Avatar answered Sep 16 '22 11:09

Manas Ranjan Mahapatra