-3

I am trying to use Spring JDBCTemplate class to access database. As a first tutorial, I have used xml spring configuration file and everything works as expected. Now, I am trying to use @Configuration and created DataSource and JdbcTemplate instances through @Bean annotation within this file. But, I get a NullPointerException at int result = template.update(sql);

I am sure I have done a silly mistake. Was wondering what it could be.

The code is as follows.

@Configuration
public class SpringJDBCAnnotation {

@Autowired
static JdbcTemplate template;

@Autowired
static DataSource dataSource;

@Bean
DataSource dataSource() {
    DriverManagerDataSource ds = new DriverManagerDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUrl("jdbc:mysql://localhost:3306/organization");
    ds.setUsername("root");
    ds.setPassword("test");
    return ds;
}

@Bean
JdbcTemplate template() {
    JdbcTemplate template = new JdbcTemplate();
    template.setDataSource(dataSource);
    return template;
}

public static void main(String[] args) {

    String sql = "insert into employee values(1, 'Tom', 'Cruise')";
    int result = template.update(sql);
    System.out.println("# of records inserted : " + result);


}

}
xingbin
  • 27,410
  • 9
  • 53
  • 103
Sara
  • 603
  • 8
  • 19
  • 5
    Because you did not start Spring. https://stackoverflow.com/questions/19896870/why-is-my-spring-autowired-field-null – xingbin Aug 31 '18 at 18:13
  • 4
    Don’t make these member fields static. Btw check out spring boot tutorials on spring.io blog. – Nathan Hughes Aug 31 '18 at 18:13
  • @NathanHughes These are accessed in the main() method and hence had to be declared as static. This is merely an exercise. – Sara Aug 31 '18 at 18:17
  • 2
    @Sara Well, it's a fine exercise in how not to use Spring, showing how Spring cannot do anything unless you initialize Spring first. You need to let Spring create *objects* that need to be autowired (implies that Spring cannot autowire `static` fields). – Andreas Aug 31 '18 at 18:20
  • 1
    Getting away from static variable evilness is a big part of why spring was created. you need to instantiate an applicationcontext like everybody is telling you, then retrieve the beans from that. – Nathan Hughes Aug 31 '18 at 18:28
  • 1
    @NathanHughes Don't retrieve beans, inject them. – chrylis -cautiouslyoptimistic- Aug 31 '18 at 18:52
  • 1
    @chrylis: yes. Retrieve one bean from the context and the rest will be injected. Again, OP: get a better tutorial. – Nathan Hughes Aug 31 '18 at 18:59
  • @NathanHughes I have answered this question. This works as intended. Can you please review it? – Sara Sep 01 '18 at 07:37

1 Answers1

0

Application Context needs to be obtained whenever we need to create beans or autowire them.

Since this is Java based configuration, it is retrieved as follows.

ApplicationContext context = new AnnotationConfigApplicationContext(SpringJDBCAnnotation.class);

and the beans needs to be accessed from the context as usual.

JdbcTemplate template = context.getBean(JdbcTemplate.class);
chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
Sara
  • 603
  • 8
  • 19