Java Gets the generic type class

Source: Internet
Author: User

To build a framework today, get generic <T> class specific examples:

@EnableTransactionManagement @transactional@repository Public classBasicdao<t>extendsHibernatedaosupport {//entity class    PrivateT entity; PrivateClass<t>Entityclass; Public    Basicdao () {  // Get generic class type Entityclass = (class<t>) through reflection mechanism    ((parameterizedtype) this. getclass (). Getgenericsuperclass ()). Getactualtypearguments () [0]; }     PublicBasicdao (T entity) { This. Entity =entity; }     Public voidSave (T entity) {Try{gethibernatetemplate (). Save (entity); } Catch(DataAccessException e) {e.printstacktrace (); }    }     Public voidUpdate (T entity) {Try{gethibernatetemplate (). Update (entity); } Catch(DataAccessException e) {e.printstacktrace (); }    }     Public voidDelete (T entity) {Assert.assertnotnull (entity); Try{gethibernatetemplate (). Delete (entity); } Catch(DataAccessException e) {e.printstacktrace (); }    }    /*** Specific primary key, return entity *@returnID*/   Public   T getmodel (int  ID) {  // get de entity model by reflection mechanism return  get    Hibernatetemplate (). get (Entityclass,id); }          /*** Automatic injection of sessionfactory *@paramsessionfactory*/@Autowired Public voidSetupsessionfactory (Finalsessionfactory sessionfactory) {         This. Setsessionfactory (sessionfactory); }}

To get a solid entity object in a common class, "see Basedao.java's Getmodel (int) method", the light has a primary key is not enough, the session also need to know the class type of the entity, and Baseaction Entityclass in the line.

How to get it? See "Constructor Basicdao ()"

Here is an example of another blog post:

 Public class Test<t> {    public  T newinstance () {        T instance=null;        Classthis. getclass (). Getgenericsuperclass ()). Getactualtypearguments () [0]; //       ...        return instance;}    ;}

Blog Address: http://www.tashan10.com/tong-guo-fan-she-huo-qu-fan-xing-de-class/

What if the class type of the calling class is changed? The classic scenario is log management.

The Java reflection mechanism can achieve the above requirements

 Public class loggerutils {    privatestatic  Logger Logger;      Public Static Logger GetLogger () {        ifnull) {            //Java8 Abandoned Reflection.getcallerclass ()            logger= logger.getlogger (reflection.getcallerclass ()) ;        }         return logger;}    }

Java Gets the generic type class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.