开发者社区> 问答> 正文

JFinal异常:The Table mapping of model not ?报错

前文1 前文2 前文3

以下是定时器初始化的代码,使用的是ServletContextListener:

public class B2pCheckListener implements ServletContextListener 
{
    Timer timer = new Timer();
    private static final Logger log = Logger.getLogger(B2pCheckListener.class);

    public void contextDestroyed(ServletContextEvent arg0)
    {
        System.out.println("系统定时任务初始化结束....");
        timer.cancel();
    }


    public void contextInitialized(ServletContextEvent arg0)
    {
        System.out.println("系统定时任务初始化开始....");
        /*调用任务,延迟30秒,每15分钟执行一次*/
        long delay = 30 * 1000;
        long intevalPeriod = 900 * 1000;
        timer.scheduleAtFixedRate(new B2pMsgCheck(), delay, intevalPeriod);
   
        Class c = getClass();  
        ClassLoader loader = c.getClassLoader();  
    }
}

以下是定时任务的代码:

public class B2pMsgCheck extends TimerTask
{
        private static final Logger log = Logger.getLogger(B2pMsgCheck.class);
	
	@Override
	public void run()
	{	
               ………………
        }
}

@JFinal 请查看。

问题补充:我把定时任务去掉后,查看启动日志如下:

项目A:

Config的ClassLoader是sun.misc.Launcher$AppClassLoader@5e9e34
Starting JFinal 1.9
Starting scanner at interval of 5 seconds.
Starting web server on port: 80
2015-09-18 15:21:10
[INFO]-[Thread: main]-[org.eclipse.jetty.server.Server.doStart()]: jetty-8.1.8.v20121106
2015-09-18 15:21:10
[INFO]-[Thread: main]-[org.eclipse.jetty.webapp.StandardDescriptorProcessor.visitServlet()]: NO JSP Support for /bocp, did not find org.apache.jasper.servlet.JspServlet
2015-09-18 15:21:11
[INFO]-[Thread: main]-[org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized()]: started o.e.j.w.WebAppContext{/bocp,file:/D:/Eclipse/workspace_j2ee/bocp/src/webapp/}
configPlugin的ClassLoader是WebAppClassLoader=15964901@f39ae5
2015-09-18 15:21:11
[INFO]-[Thread: main]-[com.mchange.v2.log.MLog.<clinit>()]: MLog clients using log4j logging.
2015-09-18 15:21:11
[INFO]-[Thread: main]-[com.mchange.v2.c3p0.C3P0Registry.banner()]: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
2015-09-18 15:21:12
[INFO]-[Thread: main]-[org.eclipse.jetty.server.AbstractConnector.doStart()]: Started SelectChannelConnector@0.0.0.0:80
Starting Complete. Welcome To The JFinal World :)
2015-09-18 15:21:14
Change的ClassLoader是WebAppClassLoader=15964901@f39ae5

问题项目:

BocpTestConfig.class的ClassLoader是sun.misc.Launcher$AppClassLoader@10014f0
Starting JFinal 1.9
Starting scanner at interval of 5 seconds.
Starting web server on port: 80
2015-09-18 15:24:12
[INFO]-[Thread: main]-[org.eclipse.jetty.server.Server.doStart()]: jetty-8.1.8.v20121106
2015-09-18 15:24:12
[INFO]-[Thread: main]-[org.eclipse.jetty.webapp.StandardDescriptorProcessor.visitServlet()]: NO JSP Support for /bocpTest, did not find org.apache.jasper.servlet.JspServlet
2015-09-18 15:24:12
[INFO]-[Thread: main]-[org.eclipse.jetty.server.handler.ContextHandler.callContextInitialized()]: started o.e.j.w.WebAppContext{/bocpTest,file:/D:/Eclipse/workspace_j2ee/bocpTest/webapp/}
configPlugin的ClassLoader是sun.misc.Launcher$AppClassLoader@10014f0
2015-09-18 15:24:13
[INFO]-[Thread: main]-[com.mchange.v2.log.MLog.<clinit>()]: MLog clients using log4j logging.
2015-09-18 15:24:13
[INFO]-[Thread: main]-[com.mchange.v2.c3p0.C3P0Registry.banner()]: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
2015-09-18 15:24:14
[INFO]-[Thread: main]-[org.eclipse.jetty.server.AbstractConnector.doStart()]: Started SelectChannelConnector@0.0.0.0:80
Starting Complete. Welcome To The JFinal World :)
2015-09-18 15:24:19
Change的ClassLoader是sun.misc.Launcher$AppClassLoader@10014f0

问题项目之所以定时任务无法正常获取Model对象,是因为它一直都是从WebAppClassLoader中寻找,但是JFinal的相关对象都在AppClassLoader,因此会报错,但是对于JFinal自身相关的运作是没有影响的,因为是在同一个ClassLoader。而另一个正常运行的项目,它的定时任务和JFinal相关对象都在WebAppClassLoader,所以都运行正常。

展开
收起
爱吃鱼的程序员 2020-06-12 11:10:03 654 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

      已经在上两个贴子中给出了解决方法:

    1: http://www.oschina.net/question/1793377_2132461#AnchorAnswer1013995

    2: http://www.oschina.net/question/1793377_251436

      注意这句话: 我现在断定你的那个定时线程起动的时机过早,将这个启动放在YourJFinalConfig.afterJFinalStart()即可解决问题。

      如果还是不知道如何解决,请看下面的载图:


    回复 @33号小人物:本质上就是同一个class被两个不同的classload各自加载了一次,在使用时造成了混乱用这个方法是可以解决问题,避免了ClassLoader不一致的问题。不过最后我找到了问题的根源,就是JFinal-1.9的jar包放在maven下,而没有直接放在WEB-INF/lib下,所以导致了WebAppClassLoader找不到相应的类,而在AppClassLoader中加载了。
    2020-06-12 11:10:20
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Apache Wicket User Guide - Ref 立即下载
introduction to Apache Bean 立即下载
POWERING PREBICTIVE MAPPING AT 立即下载