package cn.itcast.oa.base;import java.lang.reflect.parameterizedtype;import javax.annotation.resource;import cn.itcast.oa.service.departmentservice;import cn.itcast.oa.service.roleservice;import cn.itcast.oa.service.userservice;import Com.opensymphony.xwork2.actionsupport;import com.opensymphony.xwork2.modeldriven;public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T> Support for { // =============== modeldriven ================== protected t model; public baseaction () { try { // get the True type of model by reflection parameterizedtype pt = (Parameterizedtype) this.getclass (). GETGEnericsuperclass (); class<t> clazz = (class<t>) pt.getactualtypearguments () [0]; // Create an instance of model by reflection model = clazz.newinstance (); } catch (exception e) { throw new runtimeexception (e); } } public t getmodel () { return model; } // =============== service instance declarations ================== @Resource protected roleservice roleservice; @Resource protected DepartmentService departmentservice; @Resource protected UserService UserService;}
Abstract classes cannot be instantiated, so this within an abstract class refers to the instantiated object of its derived class in the actual invocation
Reference article:
http://blog.csdn.net/liang5630/article/details/40185591
This article is from the "bit accumulation" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1743536
Reflection of a generic type