最早建过一个kotlin工程,尝试了下kotlin的语法。
今天准备为我的app做一个收集日志的service时,发现eclipse中kotlin环境怎么配都无法成功。这个工程是在springboot上建立bootstrap下载下来后使用的。
仔细对比了两个工程,发现,我之前建立的工程中有kotlin builder,而新的工程中则是gradle builder。
找到原因了,在工程名上右键 configure kotlin->add native plugin,然后会添加上kotlin builder。
但是看网上信息,native plugin只是解决java和kotlin代码同时在工程中的问题。
在eclipse中没找到办法解决,在idea中尝试一下。
kotlin中报无法支持1.8的lib,在idea中可以修改kotlin支持1.8,但eclipse中未找到哪里可以修改。
build.gradle中各种插件问题修复后,在idea中可以正常使用了。现在就回到最初的问题了,在idea中运行时,怎么优先使用test/resources。
最终也没好办法,写了一个testcase来启动springboot。但在testcase中启动后就立即退出是怎么回事?查找下原因。立即即出,就在启动springboot后,sleep(xxxxxx)的时间来阻止进程进出就可以了。
目前的几种解决方案:
1、写一个testcase启动springboot,直接会使用test resources。(成功)
2、直接启动App.java或使用gradle bootrun,通过 vm parameter将环境变量传递过去。(正在测试中)
3、写一个properties文件,放在test resources下,启动时,优先去读取properties中设置的属性。(或者放在正式目录下的resources下)
关于如何传递环境变量(程序可以直接读取环境变量,因此可以不使用-D来传递)
1、jar包传递环境变量
gradle bootjar打包出来的jar包,可以用java -Dlogback.syslogHost=xxx -jar xxx.jar传递环境变量。
2、gradle bootRun 使用-D传递环境变量,需要设置以下属性。
bootRun { //classpath configurations.testRuntime systemProperties = System.properties }
3、再查看下怎么使用bootrun从properties中设置环境变量。
在junit中,会加载logback-test.properties,那能在debug模式中使用吗。只要是junit都会加载test resources。
那这样来说,debug模式本就不应该去加载test resources,也是正确的处理方式,只有test模式时才需要去加载 test resources。
费这牛鼻子劲干啥,直接写个脚本,参数通过环境变量传递不就OK啦。
或者bootrun时,能判断出来,主动去加载一个也可以。
spring boot 加载顺序
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order: Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active). @TestPropertySource annotations on your tests. @SpringBootTest#properties annotation attribute on your tests. Command line arguments. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property). ServletConfig init parameters. ServletContext init parameters. JNDI attributes from java:comp/env. Java System properties (System.getProperties()). OS environment variables. A RandomValuePropertySource that has properties only in random.*. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants). Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants). Application properties outside of your packaged jar (application.properties and YAML variants). Application properties packaged inside your jar (application.properties and YAML variants). @PropertySource annotations on your @Configuration classes. Default properties (specified by setting SpringApplication.setDefaultProperties).
还得看下spring loaded与kotlin的结合中。
还得看下devtool的作用。
下面开始测试下 devtool 与 springloaded,主要就是controller,接口增加,修改,删除是否能立即生效。
记得之前用eclipse+java app+springloaded,增加方法都支持。
在外部运行gradlew.bat bootrun
1、使用devtool
2、使用springloaded,从环境变量传入
3、使用springloaded,从build.gradle中配置
直接运行App.kt
1、好像只能使用环境变量来配置了吧。
近期评论