yangfuhai / jboot

一个优雅的微服务框架,SpringCloud 之外的另一个选择,已经使用在用户量过亿的商业产品上,有超过1000家公司在使用Jboot做极速开发...

Home Page:http://www.jboot.com.cn

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

如何不启动HTTP服务器的情况下使用数据库?

hykilpikonna opened this issue · comments

如果不用Jboot.run(args);的话, 调用数据库的Service实例的时候会报错NullPointer.
image
这里的DbKit.getConfig(HoUserSettings.class)返回了null.

每行暂停Debug了一下发现执行到
initUndertowServer:107, UnderTowServer (io.jboot.server.undertow)
这里的时候才不会返回null, 但是这样HTTP服务器已经启动了...

initUndertowServer:107, UnderTowServer (io.jboot.server.undertow)
start:192, UnderTowServer (io.jboot.server.undertow)
startServer:167, Jboot (io.jboot)
start:148, Jboot (io.jboot)
run:91, Jboot (io.jboot)
...

我的应用不需要HTTP服务器, 但是觉得这个数据库管理超级好用!
但是如果为了数据库管理启动一个多余的HTTP服务器的话好浪费性能的w..
可能在不启动HTTP服务器的情况下使用数据库嘛?

找到了, 准确位置在JFinal的 start:227, ActiveRecordPlugin (com.jfinal.plugin.activerecord) 里面!

start:227, ActiveRecordPlugin (com.jfinal.plugin.activerecord)
startPlugins:128, Config (com.jfinal.core)
configPluginWithOrder:71, Config (com.jfinal.core)
configJFinal:56, Config (com.jfinal.core)
init:63, JFinal (com.jfinal.core)
init:49, JFinalFilter (com.jfinal.core)
proceed:111, LifecyleInterceptorInvocation (io.undertow.servlet.core)
createFilter:80, ManagedFilter (io.undertow.servlet.core)
call:585, DeploymentManagerImpl$2 (io.undertow.servlet.core)
call:550, DeploymentManagerImpl$2 (io.undertow.servlet.core)
call:42, ServletRequestContextThreadSetupAction$1 (io.undertow.servlet.core)
call:43, ContextClassLoaderSetupAction$1 (io.undertow.servlet.core)
start:592, DeploymentManagerImpl (io.undertow.servlet.core)
initUndertowServer:104, UnderTowServer (io.jboot.server.undertow)
start:192, UnderTowServer (io.jboot.server.undertow)
startServer:167, Jboot (io.jboot)
start:148, Jboot (io.jboot)
run:91, Jboot (io.jboot)
main:31, JbootDBTest (cc.moecraft.icq.plugins.osubot.osu)

啊... 自己解决啦w

// 找到JFinal的Config类的configJfinal(JFinalConfig jfinalConfig)方法
Class<?> configClass = Class.forName("com.jfinal.core.Config");
Method configJFinal = configClass.getDeclaredMethod("configJFinal", JFinalConfig.class);

// 因为它的访问级别是Package Private, 需要setAccessible(true)才能调用
configJFinal.setAccessible(true);

// 用Jboot的配置文件调用
configJFinal.invoke(null, new JbootAppConfig());

这样就可以用数据库了! 但是这样启动还是好慢...
测试了, 启动用了4911ms哇...