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
}
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With