java - Autowiring a Class<T extends BaseEntity> when Autowiring a DAO bean -


so have tried searching on blocker , tried many different ways of finding solution cannot wrap head around it. beginner in spring , have created generic dao takes in t extends baseentity. have following code:

    private class<t> clazz; @autowired public void setclazz(class<t> clazz){     system.out.println("in class autowired setter");     this.clazz = clazz; }  @bean @scope(beandefinition.scope_prototype) public class<t> getclazz(){     return clazz; } 

which have put inside basedaoimpl. use constructor:

public basedaoimpl() {     this.collection = mongodbutil.getcollection(mongoconnection.mongodb(), clazz); } 

to create jacksondbcollection mongodb. in customerserviceimpl autowire basedao bean can use dao persistance.

  @autowired public void setbasedao(basedao<customerentity> basedao) {     this.basedao = basedao; } 

so when run method uses basedaoimpl, nullpointerexception , pretty sure because of class not picking customerentity. question how can class pick customerentity when autowire basedao bean? or suggestions have me solve problem? need different point of view , thought ask here.

thank in advanced assistance.

update

basedaoimpl

package za.co.thekeeper.dao; import org.mongojack.dbcursor; import org.mongojack.dbquery; import org.mongojack.jacksondbcollection; import org.mongojack.writeresult; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.config.beandefinition;  import org.springframework.context.annotation.bean; import org.springframework.context.annotation.scope; import org.springframework.stereotype.repository; import za.co.thekeeper.entities.baseentity; import za.co.thekeeper.mongo.mongoconnection; import za.co.thekeeper.mongo.mongodbutil; import java.util.arraylist; import java.util.list;  @repository @scope(beandefinition.scope_prototype) public class basedaoimpl<t extends baseentity> implements basedao<t> {  private jacksondbcollection<t, string> collection;  /**dependency injections**/ private mongoconnection mongoconnection; @autowired public void setmongoconnection(mongoconnection mongoconnection) {     system.out.println("in autowired setter");     this.mongoconnection = mongoconnection; }  private class<t> clazz; @autowired public void setclazz(class<t> clazz){     system.out.println("in class autowired setter");     this.clazz = clazz; }  @bean @scope(beandefinition.scope_prototype) public class<t> getclazz(){     return clazz; }  public basedaoimpl() {     this.collection = mongodbutil.getcollection(mongoconnection.mongodb(), clazz); }  @override public t create(t entity) {     entity.activate();     entity.beforepersist();     writeresult<t, string> inserted = this.collection.insert(entity);     return inserted.getsavedobject(); }  @override public void delete(t entity) {     deactivate(entity); }  @override public t activate(t entity) {     entity.activate();     return update(entity); }  @override public t deactivate(t entity) {     entity.deactivate();     return update(entity); }  @override public void activate(string id) {     t entity = getbyid(id);      if (entity == null) {         throw new nullpointerexception("entity not found id: " + id);     }      activate(entity); }  @override public void deactivate(string id) {     t entity = getbyid(id);      if (entity == null) {         throw new nullpointerexception("entity not found id: " + id);     }      deactivate(entity); }  @override public t update(t entity) {     entity.beforepersist();     writeresult<t, string> saved = this.collection.save(entity);     return saved.getsavedobject(); }  @override public void deletebyid(string id) {     deactivate(id);     update(getbyid(id)); }  @override public t getbyid(string id) {     return this.collection.findonebyid(id); }  @override public list<t> findbyfield(string field, object o) {     dbquery.query query = dbquery.is(field, o);     dbcursor<t> cursor = this.collection.find(query);     return readcursor(cursor); }  @override public void createdbref(string databasename, string collectionname, string id) {  }  @override public list<t> findall() {     return findbyfield("activate", boolean.true); }  /**  * cursor  **/ private list<t> readcursor(dbcursor<t> cursor) {     if (cursor.size() > 0) {         list<t> found = new arraylist<>();         while (cursor.hasnext()) {             found.add(cursor.next());         }         return found;     }     return null; } } 

customerserviceimpl

package za.co.thekeeper.service;   import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.config.beandefinition; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.scope; import org.springframework.stereotype.service; import za.co.thekeeper.dao.basedao; import za.co.thekeeper.dao.basedaoimpl; import za.co.thekeeper.entities.customerentity; import za.co.thekeeper.entities.merchantentity; import za.co.thekeeper.entities.receiptentity; import za.co.thekeeper.mongo.mongoconnection;  import java.util.list; import java.util.uuid;  @service  public class customerserviceimpl implements customerservice {  private basedao<customerentity> basedao;  @autowired public void setbasedao(basedao<customerentity> basedao) {     this.basedao = basedao; }  @override public customerentity registernewcustomer(customerentity customer) {     if (customer.getid() == null) {         customer.setid(uuid.randomuuid().tostring());     }      customerentity entity = basedao.create(customer);     system.out.println(entity.tostring());     return entity; }  @override public customerentity getcustomer(string cellnumber) {     list<customerentity> entities = basedao.findbyfield("cellnumber", cellnumber);     return entities.get(0); }  @override public customerentity updatecustomerdetails(customerentity customer) {      return basedao.update(customer); }  @override public void deletecustomer(customerentity customer) {     basedao.delete(customer); } } 

application context

<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"    xsi:schemalocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-4.0.xsd">  <context:annotation-config/>  <context:component-scan base-package="za.co.thekeeper"/> 

as requested.

new update

basedao

package za.co.thekeeper.dao;  import za.co.thekeeper.entities.baseentity; import za.co.thekeeper.mongo.mongoconnection;  import java.util.list;  public interface basedao<t extends baseentity> {  t create(t entity);  void delete(t entity);  t activate(t entity);  t deactivate(t entity);  void activate(string id);  void deactivate(string id);  t update(t entity);  void deletebyid(string id);  t getbyid(string id);  list<t> findbyfield(string id, object o);  void createdbref(string databasename, string collectionname, string id);  list<t> findall(); } 

you existing basedaoimpl:

private mongoconnection mongoconnection; @autowired public void setmongoconnection(mongoconnection mongoconnection) {     system.out.println("in autowired setter");     this.mongoconnection = mongoconnection; }  private class<t> clazz; @autowired public void setclazz(class<t> clazz){     system.out.println("in class autowired setter");     this.clazz = clazz; }  @bean @scope(beandefinition.scope_prototype) public class<t> getclazz(){     return clazz; }  public basedaoimpl() {     this.collection = mongodbutil.getcollection(mongoconnection.mongodb(), clazz); } 

this not right way initialize bean.

  1. you doing setter injection on mongoconnection , accessing in default constructor. mongodbutil.getcollection(mongoconnection.mongodb(), clazz); constructor gets called before setter invoked. going result in npe.
  2. nobody setting clazz variable.
  3. @autowired on setter , @bean on getter meaningless.
  4. @repository annotation not required. can use @component

the right way things this.

basedaoimpl class - including part of class brevity

package za.co.thekeeper.dao; import org.mongojack.dbcursor; import org.mongojack.dbquery; import org.mongojack.jacksondbcollection; import org.mongojack.writeresult; import org.springframework.beans.factory.annotation.autowired; import org.springframework.beans.factory.config.beandefinition;  import org.springframework.context.annotation.bean; import org.springframework.context.annotation.scope; import org.springframework.stereotype.repository; import za.co.thekeeper.entities.baseentity; import za.co.thekeeper.mongo.mongoconnection; import za.co.thekeeper.mongo.mongodbutil; import java.util.arraylist; import java.util.list;  public class basedaoimpl<t extends baseentity> implements basedao<t> {  private jacksondbcollection<t, string> collection;  /**dependency injections**/ private mongoconnection mongoconnection;  public void setmongoconnection(mongoconnection mongoconnection) {     system.out.println("in autowired setter");     this.mongoconnection = mongoconnection; }  private class<t> clazz;  public void setclazz(class<t> clazz){     system.out.println("in class autowired setter");     this.clazz = clazz; }  public class<t> getclazz(){     return clazz; }  public basedaoimpl(mongocollection mongocollection, class<t> clazz) {     this.mongocollection = mongocollection;     this.clazz = clazz;     this.collection = mongodbutil.getcollection(mongoconnection.mongodb(), clazz); } } 

spring configuration:

<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"    xsi:schemalocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd    http://www.springframework.org/schema/context      http://www.springframework.org/schema/context/spring-context-4.0.xsd">  <context:annotation-config/>  <context:component-scan base-package="za.co.thekeeper"/>  <bean class="za.co.thekeeper.dao.basedaoimpl">     <constructor-arg index="0" ref="mongocollection"/>     <constructor-arg index="1">         <value type="java.lang.class">customerentity</value>     </constructor-arg> </bean> 

spring detect type t second constructor injection , allow autowire beans of type basedaoimpl<customerentity>.

note: assume mongocollection declared spring bean elsewhere. otherwise need add in spring configuration file.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -