java - Spring injecting or autowiring datasource bean to class -


this may novice question, have searched , either have large gap in understanding or doing incorrectly cannot figure out.

in context file here excerpt

<bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource">     <property name="driverclassname" value="${datasource.driverclassname}" />     <property name="url" value="${datasource.url}" />     <property name="username" value="${datasource.username}" />     <property name="password" value="${datasource.password}" /> </bean>  <bean id="mybeanone" class="a.b.c.mybeanone">          <property name="datasource" ref="datasource" /> </bean> 

now in mybeanone have:

private datasource datasource;  private jdbctemplate jdbctemplate;  @autowired public void setdatasource (datasource datasource) {     this.jdbctemplate = new jdbctemplate(datasource); }  public void mymethod() {     string sql = "'my generic sql update query'";     try {         this.jdbctemplate.update(sql);     } catch (org.springframework.dao.emptyresultdataaccessexception ex) {     }     system.exit(0); } 

when try execute on line setdatasource invoked error:

error org.springframework.integration.handler.logginghandler      org.springframework.integration.messagehandlingexception:         java.lang.nullpointerexception 

on line: this.jdbctemplate.update(sql);

i have tried maybe ten different configurations work, cannot seem it. assistance appreciated, thank you.

edit: per luiggi's comment:

//in yet classes run method mybeanone bone = someotherclass.create();   //just returns new mybeanone bone.mymethod(); 

neither someotherclass or class classified beans in context or have presence in context.

i know basic question struggling it.

thank patience.

as noted in comments, problem you're manually creating bean instead of letting spring container create it. basically, you're doing this:

new mybeanone() 

so spring container can't inject of fields have configured being null e.g. jdbctemplate field. there solutions this:

  1. convert someotherclass bean managed spring container , let inject mybeanone instance (probably using @autowired annotation).

  2. if latter approach can't done since need manually create bean, can create bean manually shown here: how create spring beans dynamically?

    but implementation makes hardcode somewhere spring config file name , use in code. so, better approach option 3.

  3. look @ solution: creating new spring beans on demand, create client abstract class method spring implement retrieve new instance of spring managed bean.


i found way handle using @configurable annotation. decorating bean annotation, can create new instance of bean on demand , spring manage injection of spring managed beans you. achieve this, spring needs use aspects behind scenes , should activate usage of aspects project. explanation quite long, provide links explain in depth solution:

note in order enable feature, have add java agent when starting jvm weave class @ runtime using aspects.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -