Spring 4.0 supports generic dependency injection, which is implemented as follows
Public abstract class Genericservice<t extends Basebean, PK extends serializable> implements Baseservice<t, PK & Gt {//@SuppressWarnings ("unchecked")//Private class<t> Gettclass () {//Return ((class<t>) ((Parame Terizedtype) GetClass (). Getgenericsuperclass ()). Getactualtypearguments () [0]);////}/** * spring4.0 support for generic injection * Since autowired is a dependency type * 4.0 previously considered to be of the same type, subclasses inherit Genericservice and will be considered to have multiple identical Genericdao instances so that the error */@Autowired public genericdao<t, Pk> DAO; public void Save (T t) {if (t = = null) {return; } this.dao.insert (t); public void Saveorupdate (T-t) {if (t = = null) {return; } if (t.id== null) {This.save (t); } else {this.update (t); }}/** * Modify record * According to param condition * @param param used to generate SQL parameter values, including where condition, Target field and new value, etc. * @throws Exception */ @Transactional public void Update (map<string, object> param) {if(param = = null| | Param.size () ==0) {return; } this.dao.update (param); }/** * Modify entity */@Transactional public void update (T t) {if (t = = null) {return; } this.dao.update (t); }/** * Delete entity * * @param ID * @throws Exception */@Transactional public void Delete (PK id) { if (id = = null) {return; } this.dao.delete (ID); /** * Delete entity * According to param condition * @param param used to generate the parameter value of SQL, including the Where condition, Target field and new value, etc. * @throws Exception */@Tr ansactional public void Delete (map<string, object> param) {if (param = = null| | Param.size () ==0) {return; } this.dao.delete (param); /** * Get entity by ID */@SuppressWarnings ("unchecked") public T get (PK ID) {if (id = = NULL) { return null; } t = This.dao.get (ID); return t; }/** * Query entity * based on param condition * @pAram param used to generate SQL parameter values, including where condition, Target field and new value, etc. * @throws Exception */Public T get (map<string, object> param) {if (param = = null| | Param.size () ==0) {return null; } t = This.dao.get (param); return t; }/** * Take all records * @return all record entity Objects list */public list<t> list () {return this.dao.select (); /** * Query records by criteria * @param param query condition parameters, including where condition, page condition, sort condition * @return List */public for entity objects that match the condition record List<t> Select (map<string, object> param) {return this.dao.select (param); }/** * Query the total number of records for the whole table */public int count () {return this.dao.count (); /** * Query the number of records that meet the criteria * @param param query condition parameters, including the Where condition (the contents of other parameters do not work). This parameter is set to NULL, which is equivalent to count () * @return */public int count (map<string, object> param) {if (param = = null| | Param.size () ==0) {return 0; } return This.dao.count (param); }}
Basedao injection problem during General Service authoring