对象不是声明类的一个实例。

//获取adminLogService对象
        Object obj = null;
        ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(request.getServletContext()); 
        obj = context.getBean("adminLogService"); 
        
        if (obj != null) {
    	    try{
    	    	//使用反射执行adminLogService对象save方法
    	        Class<AdminLogService> adminLogService = (Class<AdminLogService>) obj.getClass();
    	        Method save = adminLogService.getDeclaredMethod("save", new Class[] { SystemLogEntity.class });
    	        save.invoke(adminLogService, new Object[] { systemLogEntity });
    	    }catch (Exception e){
    	        e.printStackTrace();
    	    }
        }
        

执行到save.invoke(adminLogService, new Object[] { systemLogEntity });时报错!

报错:object is not an instance of declaring class  

说明Class没有实例化; 


解决办法: 

由于没有实例化可以有如下两种方法: 
1、反射方法定义成为static的,故被反射类就不需要实例化; 

2、method.invoke(_class.newInstance(), args);

save.invoke(adminLogService.newInstance(), new Object[] { systemLogEntity });

Logo

基于 Vue 的企业级 UI 组件库和中后台系统解决方案,为数万开发者服务。

更多推荐