0

Im trying to load properties in Spring from a properties file. But I keep getting a null value. Here's my program:

    public class App {
        @Value("${jdbc.url}")
        private  String jdbcUrl;



        public static void main(String[] args) {
            ApplicationContext context = new ClassPathXmlApplicationContext(
                    "Spring-All-Modules.xml");// load super configuration file.

            App app= new App();
            System.out.println("URL"+ app.jdbcUrl);// always print null .

Im loading the properties file in the application context as follows:

      <context:property-placeholder location="/databaseconfig.properties"/>

And this is my database properties file :

jdbc.driverClassName=com.cassandra.datastax.Driver
jdbc.url=jdbc:url

How do I fix this?

EDIT: The question is not a duplicate. The other question uses autowired and a configuration class. This uses neither. My example prints null the other throws a null pointer exception. Please open this question.

seeker
  • 6,841
  • 24
  • 64
  • 100
  • Please read more carefully. The problem is that you are creating the instance yourself rather than getting it from the `ApplicationContext`. The same behavior applies to `@Autowired` as it does to `@Value`. `null` eventually causes `NullPointerException`. – Sotirios Delimanolis Sep 08 '14 at 22:24
  • Conceptually .Maybe. However, the answers to the other question do not help solve this one. – seeker Sep 08 '14 at 22:38
  • They absolutely do. Declare a `App` bean (and enable component scanning) in your context and retrieve it from the `ApplicationContext`, rather than instantiating it yourself. – Sotirios Delimanolis Sep 08 '14 at 22:40
  • Here you go: http://stackoverflow.com/questions/4130486/spring-value-annotation-always-evaluating-as-null. The solutions are the same. You have to read between the lines. – Sotirios Delimanolis Sep 08 '14 at 22:45

0 Answers0