Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to read values from property file using @value or by using Autowired to Enviroment

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

like image 864
Aman Vyas Avatar asked Oct 29 '25 07:10

Aman Vyas


1 Answers

You're missing a dollar sign in Value annotation. This should do the work:

@Value("${prop.name}")
like image 87
Andronicus Avatar answered Oct 30 '25 23:10

Andronicus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!