java - Dependency Injection of datasource and jdbctemplate? -


i new webapp development in general , spring framework in particular. following spring jdbc transactions tutorial instead of accessing db service 1 class, wish access multiple classes.

in tutorial service defined this

public class bookingservice {      @autowired     jdbctemplate jdbctemplate;      @transactional     public void book(string... persons) {         (string person : persons) {             system.out.println("booking " + person + " in seat...");             jdbctemplate.update("insert bookings(first_name) values (?)", person);         }     };      public list<string> findallbookings() {         return jdbctemplate.query("select first_name bookings", new rowmapper<string>()                  @override             public string maprow(resultset rs, int rownum) throws sqlexception {                 return rs.getstring("first_name");             }         });     }  } 

these beans

@configuration @enabletransactionmanagement @enableautoconfiguration public class application {  @bean bookingservice bookingservice() {     return new bookingservice(); }  @bean datasource datasource() {     return new simpledriverdatasource() {{         setdriverclass(org.h2.driver.class);         setusername("sa");         seturl("jdbc:h2:mem");         setpassword("");     }}; }  @bean jdbctemplate jdbctemplate(datasource datasource) {     jdbctemplate jdbctemplate = new jdbctemplate(datasource);     system.out.println("creating tables");     jdbctemplate.execute("drop table bookings if exists");     jdbctemplate.execute("create table bookings(" +             "id serial, first_name varchar(5) not null)");     return jdbctemplate; } 

they instantiated service in application class , did transactions there

applicationcontext ctx = springapplication.run(application.class, args); bookingservice bookingservice = ctx.getbean(bookingservice.class);  //bookingservice.dostuff() 

in test project, copied same bean definitions performed transactions in multiple classes.

public class foo {     applicationcontext ctx = new annotationconfigapplicationcontext(application.class);     bookingservice bookingservice = ctx.getbean(bookingservice.class);     bookingservice.book(...);     // other stuff }  public class bar {     applicationcontext ctx = new annotationconfigapplicationcontext(application.class);     bookingservice bookingservice = ctx.getbean(bookingservice.class);     bookingservice.findallbookings(...);     // other stuff } 

it seems when perform transactions in 1 class (for example, book , find in foo class) performs expected. when try separate them multiple classes doesn't behave expected. if perform book in foo, cannot find in bar. concept missing? instantiating multiple instances of datasource , jdbctemplate because instantiated service multiple times. thought spring handles injection? , since there 1 physical database there 1 instance of datasource , jdbctemplate. concept did misunderstood? please , point me right direction. thanks.

you need inject dependencies, example:

public class foo {      @autowired     private bookingservice bookingservice;      public dostuff() {         bookingservice.book(...);         // other stuff     } } 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -