0

Here are my spring.datasource properties in application.properties-

        spring.datasource.url=jdbc:postgresql://<hostname goes here>
        spring.datasource.username=<username goes here>
        spring.datasource.password=password
        spring.datasource.driverClassName=org.postgresql.Driver

My main class is as follows:

        @PropertySources(value = {@PropertySource("classpath:application.properties")})
        @PropertySource(value = "classpath:sql.properties")
        @SpringBootApplication
        public class MyApp implements CommandLineRunner{

            public static void main(String[] args) {

                SpringApplication.run(MyApp.class, args);
            }

            @Override
            public void run(String... strings) throws Exception {
                Execute execute = new Execute();
                execute.executeCleanUp();

            }
        }

The Execute class is as follows:

    import com.here.oat.repository.CleanUpEntries;
    import com.here.oat.repository.CleanUpEntriesImpl;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;

    import java.io.IOException;

    /***
     *
     */

    public class Execute {

        @Autowired
        private CleanUpEntries cleanUpEntries;


        public void executeCleanUp() throws IOException {
            cleanUpEntries = new CleanUpEntriesImpl();
            cleanUpEntries.delete();
        }
    }

Here is the implementation class - CleanupEntriesImpl:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class CleanUpEntriesImpl implements CleanUpEntries{

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Value(value = "${delete.query}")
    private String deleteQuery;

    @Override
    public int delete() {
        int id= jdbcTemplate.queryForObject(deleteQuery, Integer.class);
       return id;
    }

}

pom.xml has the following dependencies:

 <!--jdbc driver-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>9.4-1201-jdbc41</version>
            <scope>runtime</scope>
        </dependency>

Not sure why jdbcTemplate object is null when the delete() method is called. Any ideas?

Tisha
  • 827
  • 3
  • 11
  • 34
  • I haven't used spring-boot but where is your jdbcTemplate getting created? You would need to pass the data source to that right? – uncaught_exception Feb 02 '16 at 17:34
  • Possible duplicate of [Why is my Spring @Autowired field null?](http://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null) – kryger Feb 02 '16 at 21:33

1 Answers1

0

The issue was resolved by removing all new operators from my classes and autowiring everything and making Execute a Component class.

Thanks!

Tisha
  • 827
  • 3
  • 11
  • 34