There are many operations in the DAO layer that can be implemented with paradigm and reflection, so I began to conceive my balm Basedao.
Basehibernatedaoadvance.java
Public classBasehibernatedaoadvance<textendsModifyinfoentity, PKextendsSerializable, DTOextendsBasedto>extendsHibernatedaosupportImplementsBasedaoadvance<t, PK, dto> { Private FinalLogger Logger =Loggerfactory.getlogger (GetClass ()); @AutowiredPrivate voidgetsessionfactory (sessionfactory sessionfactory) {Super. Setsessionfactory (sessionfactory); } //entity class type (automatically assigned by construction method) PrivateClass<t>Entityclass; PrivateClass<dto>Dtoclass; //constructor method, automatically gets the entity class type according to the instance class Publicbasehibernatedaoadvance () {System.out.println ("-------------->in basehibernatedaoadvance"); This. Entityclass =NULL; Class C=getclass (); System.out.println (c); Type T=C.getgenericsuperclass (); System.out.println (t); if(tinstanceofParameterizedtype) {System.out.println ("In If"); Type[] P=( (Parameterizedtype) t). Getactualtypearguments (); System.out.println (Arrays.tostring (p)); This. Entityclass = (class<t>) p[0]; This. Dtoclass = (class<dto>) p[2]; } }}
Write and prepare some test classes.
@RunWith (Springjunit4classrunner.class) @ContextConfiguration (Locations= {"Classpath:/config/applicationcontext.xml","Classpath:/config/service.xml", "Classpath:/config/dao.xml" }) Public classBasedaoimpladvancetest {PrivateBasedaoadvance<course, Serializable, coursedto>Basedaoimpl; PublicBasedaoadvance<course, Serializable, coursedto>Getbasedaoimpl () {returnBasedaoimpl; } @Autowired Public voidSetbasedaoimpl (basedaoadvance<course, Serializable, coursedto>Basedaoimpl) { This. Basedaoimpl =Basedaoimpl; } @Test Public voidquerylist () {coursedto dto=Newcoursedto (); Basedaoimpl.querylist (DTO); System.out.println ("Here-------------------->"); }}
But prompt error java.lang.NullPointerException
Look at the code and find out that this section of judgment doesn't pass.
if (t instanceof Parameterizedtype) { System.out.println ("in If"); Type[] p = ((Parameterizedtype) t). Getactualtypearguments (); System.out.println (Arrays.tostring (P)); This.entityclass = (class<t>) p[0]; This.dtoclass = (class<dto>) p[2]; }
Remove if test, or error,Java.lang.ClassCastException:java.lang.Class cannot is cast to Java.lang.reflect.ParameterizedType
~ ~ ~ (>_<) ~ ~ ~
The reason is because the paradigm is not used in the right way.
Added specific DAO and implementation
Public Interface extends Basedaoadvance<course, serializable,coursedto> {}
@Repository Public class extends Implements Coursedao {}
Test class
@RunWith (Springjunit4classrunner.class) @ContextConfiguration (Locations= {"Classpath:/config/applicationcontext.xml","Classpath:/config/service.xml", "Classpath:/config/dao.xml" }) Public classCoursedaoimpltest {//private Basedaoadvance<course, Serializable, coursedto> Basedaoimpl;@AutowiredPrivateCoursedao Coursedao; @Test Public voidquerylist () {coursedto dto=Newcoursedto (); Coursedao.querylist (DTO); System.out.println ("Here-------------------->"); }}
OK, Success!
The paradigm should be specified at compile time, not at runtime.
Java.lang.Class cannot is cast to Java.lang.reflect.ParameterizedType