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> { // =============== ModelDriven的支持 ================== protected T model; public BaseAction() { try { // 通过反射获取model的真实类型 ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); Class<T> clazz = (Class<T>) pt.getActualTypeArguments()[0]; // 通过反射创建model的实例 model = clazz.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } } public T getModel() { return model; } // =============== Service实例的声明 ================== @Resource protected RoleService roleService; @Resource protected DepartmentService departmentService; @Resource protected UserService userService; }
抽象类是不能实例化的,所以抽象类里面的this是指实际调用中它的派生类的实例化对象
参考文章:
http://blog.csdn.net/liang5630/article/details/40185591
时间: 2024-10-13 09:47:54