I have two properties file in my Spring boot project. And I am able to read the properties from both in one class. But the same value when I am trying to read from a different class using @Value or by Autowired Environment, it is giving me null.
prop.name=test /* property file value */
@Component
public class TestUtil { // This is the class giving me null value
@Value("${prop.name}")
String st;
public String getTestString()
{
System.out.println(st+ " ***");
return st;
}
}
//Using @Autowired Enviroment
public class TestUtil {
@Autowired
private Environment env;
public String getTestString()
{
System.out.println(env.getProperty("prop.name")+ " ***");
return env.getProperty("prop.name");
}
}
/* Class below giving me value from properties file*/
public class JsonWriter extends JsonResponseWriter {
@Value("${prop.name}")
private String contentsMenus;
/* Some method*/
System.err.println("from JsonWriter "+contentsMenus);
Here I am autowiring
@Service
public class ResponseUtil {
@Autowired
private TestUtil util ;
In the above class I am using autowired
You're missing a dollar sign in Value annotation. This should do the work:
@Value("${prop.name}")
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