NutzCN Logo
问答 在spring booo项目中集成nutz dao后不能使用devtools
发布于 2584天前 作者 qq_a3440c18 5244 次浏览 复制 上一个帖子 下一个帖子
标签: nutzwk

在spring boot项目中通过nutz-plugins-spring-boot-starter插件集成了nutz dao,可以正常使用。
但是在POM文件中加入spring-boot-devtools的依赖,在代码修改编译后自动重启应用程序时,抛出java.lang.ClassCastException异常,导致应用启动失败。
抛异常的代码如下

    public Map<String, String> queryAllToMap() {
        List<Config> configs = queryAll();
        Map<String, String> map = new HashMap<>();
        for (Config config : configs) {    // 这里抛出异常
            map.put(config.getName(), config.getValue());
        }

        return map;
    }

异常日志如下;

[2017-10-19 09:51:39,086] o.s.b.SpringApplication ERROR - Application startup failed
java.lang.ClassCastException: cn.cytong.pay.model.Config cannot be cast to cn.cytong.pay.model.Config
	at cn.cytong.pay.service.ConfigService.queryAllToMap(ConfigService.java:27) ~[classes/:na]
	at cn.cytong.pay.ApplicationStartup.onApplicationEvent(ApplicationStartup.java:47) ~[classes/:na]
	at cn.cytong.pay.ApplicationStartup.onApplicationEvent(ApplicationStartup.java:26) ~[classes/:na]
12 回复

贴完整报错信息看看

[2017-10-19 09:51:39,050] c.c.p.ApplicationStartup INFO - init app
[2017-10-19 09:51:39,051] c.c.p.ApplicationStartup INFO - 加载epay配置信息 : merchantPrivateKey,notifyUrl,merchantPwd,merchantPublicKey
where to go 9
[2017-10-19 09:51:39,080] o.a.c.c.StandardService INFO - Stopping service [Tomcat]
[2017-10-19 09:51:39,084] o.s.b.a.l.AutoConfigurationReportLoggingInitializer INFO - 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
[2017-10-19 09:51:39,086] o.s.b.SpringApplication ERROR - Application startup failed
java.lang.ClassCastException: cn.cytong.pay.model.Config cannot be cast to cn.cytong.pay.model.Config
	at cn.cytong.pay.service.ConfigService.queryAllToMap(ConfigService.java:27) ~[classes/:na]
	at cn.cytong.pay.ApplicationStartup.onApplicationEvent(ApplicationStartup.java:47) ~[classes/:na]
	at cn.cytong.pay.ApplicationStartup.onApplicationEvent(ApplicationStartup.java:26) ~[classes/:na]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:883) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
	at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
	at cn.cytong.pay.CytongPayApplication.main(CytongPayApplication.java:10) [classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_111]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_111]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_111]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_111]
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.6.RELEASE.jar:1.5.6.RELEASE]

可能要重置一下NutDao的'EntityHolder

spring boot有没有reload时候触发的接口, 然后调用一下下面的语句

dao.getEntityHolder().clear();

解决问题了,是由于classloader不一致造成的,这里讲的比较详细:https://github.com/abel533/MyBatis-Spring-Boot/issues/5
解决办法是在resources/META-INF目录下增加spring-devtools.properties文件,内容如下:

restart.include.nutzdao=/nutz-plugins-spring-boot-starter-1.r.62.jar
restart.include.nutz=/nutz-1.r.62.jar

上面的配置可以让devtools使用RestartClassLoader来加载nutz相关的jar包

建议在nutz-plugins-spring-boot-starter插件的文档里添加上面的说明,我也是隐约记得beetl集成的时候要配置这个东西才想起来去搜索devtools配置的。

这个文件是不是应该包含在插件内呢?

应该可以,看这里https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html

All META-INF/spring-devtools.properties from the classpath will be loaded. You can package files inside your project, or in the libraries that the project consumes.

能给nutzmore来个pull request不(;`O´)o

github或码云都可以

https://github.com/nutzam/nutzmore
https://gitee.com/nutz/nutzmore

已经在码云发了pull request。

添加回复
请先登陆
回到顶部