{"id":2186,"date":"2018-03-14T06:20:11","date_gmt":"2018-03-14T06:20:11","guid":{"rendered":"http:\/\/blog.zhukunqian.com\/?p=2186"},"modified":"2018-04-24T13:40:23","modified_gmt":"2018-04-24T13:40:23","slug":"kotlin%e5%b7%a5%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/blog.zhukunqian.com\/?p=2186","title":{"rendered":"kotlin\u5de5\u7a0b"},"content":{"rendered":"<p>\u6700\u65e9\u5efa\u8fc7\u4e00\u4e2akotlin\u5de5\u7a0b\uff0c\u5c1d\u8bd5\u4e86\u4e0bkotlin\u7684\u8bed\u6cd5\u3002<\/p>\n<p>\u4eca\u5929\u51c6\u5907\u4e3a\u6211\u7684app\u505a\u4e00\u4e2a\u6536\u96c6\u65e5\u5fd7\u7684service\u65f6\uff0c\u53d1\u73b0eclipse\u4e2dkotlin\u73af\u5883\u600e\u4e48\u914d\u90fd\u65e0\u6cd5\u6210\u529f\u3002\u8fd9\u4e2a\u5de5\u7a0b\u662f\u5728springboot\u4e0a\u5efa\u7acbbootstrap\u4e0b\u8f7d\u4e0b\u6765\u540e\u4f7f\u7528\u7684\u3002<\/p>\n<p>\u4ed4\u7ec6\u5bf9\u6bd4\u4e86\u4e24\u4e2a\u5de5\u7a0b\uff0c\u53d1\u73b0\uff0c\u6211\u4e4b\u524d\u5efa\u7acb\u7684\u5de5\u7a0b\u4e2d\u6709kotlin builder\uff0c\u800c\u65b0\u7684\u5de5\u7a0b\u4e2d\u5219\u662fgradle builder\u3002<\/p>\n<p>\u627e\u5230\u539f\u56e0\u4e86\uff0c\u5728\u5de5\u7a0b\u540d\u4e0a\u53f3\u952e configure kotlin-&gt;add native plugin\uff0c\u7136\u540e\u4f1a\u6dfb\u52a0\u4e0akotlin builder\u3002<\/p>\n<p>\u4f46\u662f\u770b\u7f51\u4e0a\u4fe1\u606f\uff0cnative plugin\u53ea\u662f\u89e3\u51b3java\u548ckotlin\u4ee3\u7801\u540c\u65f6\u5728\u5de5\u7a0b\u4e2d\u7684\u95ee\u9898\u3002<\/p>\n<p>\u5728eclipse\u4e2d\u6ca1\u627e\u5230\u529e\u6cd5\u89e3\u51b3\uff0c\u5728idea\u4e2d\u5c1d\u8bd5\u4e00\u4e0b\u3002<\/p>\n<p>kotlin\u4e2d\u62a5\u65e0\u6cd5\u652f\u63011.8\u7684lib\uff0c\u5728idea\u4e2d\u53ef\u4ee5\u4fee\u6539kotlin\u652f\u63011.8\uff0c\u4f46eclipse\u4e2d\u672a\u627e\u5230\u54ea\u91cc\u53ef\u4ee5\u4fee\u6539\u3002<\/p>\n<p>build.gradle\u4e2d\u5404\u79cd\u63d2\u4ef6\u95ee\u9898\u4fee\u590d\u540e\uff0c\u5728idea\u4e2d\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528\u4e86\u3002\u73b0\u5728\u5c31\u56de\u5230\u6700\u521d\u7684\u95ee\u9898\u4e86\uff0c\u5728idea\u4e2d\u8fd0\u884c\u65f6\uff0c\u600e\u4e48\u4f18\u5148\u4f7f\u7528test\/resources\u3002<\/p>\n<p>\u6700\u7ec8\u4e5f\u6ca1\u597d\u529e\u6cd5\uff0c\u5199\u4e86\u4e00\u4e2atestcase\u6765\u542f\u52a8springboot\u3002\u4f46\u5728testcase\u4e2d\u542f\u52a8\u540e\u5c31\u7acb\u5373\u9000\u51fa\u662f\u600e\u4e48\u56de\u4e8b?\u67e5\u627e\u4e0b\u539f\u56e0\u3002\u7acb\u5373\u5373\u51fa\uff0c\u5c31\u5728\u542f\u52a8springboot\u540e\uff0csleep(xxxxxx)\u7684\u65f6\u95f4\u6765\u963b\u6b62\u8fdb\u7a0b\u8fdb\u51fa\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u76ee\u524d\u7684\u51e0\u79cd\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<p>1\u3001\u5199\u4e00\u4e2atestcase\u542f\u52a8springboot\uff0c\u76f4\u63a5\u4f1a\u4f7f\u7528test resources\u3002(\u6210\u529f)<\/p>\n<p>2\u3001\u76f4\u63a5\u542f\u52a8App.java\u6216\u4f7f\u7528gradle bootrun\uff0c\u901a\u8fc7 vm parameter\u5c06\u73af\u5883\u53d8\u91cf\u4f20\u9012\u8fc7\u53bb\u3002(\u6b63\u5728\u6d4b\u8bd5\u4e2d)<\/p>\n<p>3\u3001\u5199\u4e00\u4e2aproperties\u6587\u4ef6\uff0c\u653e\u5728test resources\u4e0b\uff0c\u542f\u52a8\u65f6\uff0c\u4f18\u5148\u53bb\u8bfb\u53d6properties\u4e2d\u8bbe\u7f6e\u7684\u5c5e\u6027\u3002(\u6216\u8005\u653e\u5728\u6b63\u5f0f\u76ee\u5f55\u4e0b\u7684resources\u4e0b)<\/p>\n<p>&nbsp;<\/p>\n<p>\u5173\u4e8e\u5982\u4f55\u4f20\u9012\u73af\u5883\u53d8\u91cf(\u7a0b\u5e8f\u53ef\u4ee5\u76f4\u63a5\u8bfb\u53d6\u73af\u5883\u53d8\u91cf\uff0c\u56e0\u6b64\u53ef\u4ee5\u4e0d\u4f7f\u7528-D\u6765\u4f20\u9012)<\/p>\n<p>1\u3001jar\u5305\u4f20\u9012\u73af\u5883\u53d8\u91cf<\/p>\n<pre class=\"brush: bash; gutter: true\">gradle bootjar\u6253\u5305\u51fa\u6765\u7684jar\u5305\uff0c\u53ef\u4ee5\u7528java -Dlogback.syslogHost=xxx -jar xxx.jar\u4f20\u9012\u73af\u5883\u53d8\u91cf\u3002<\/pre>\n<p>2\u3001gradle bootRun \u4f7f\u7528-D\u4f20\u9012\u73af\u5883\u53d8\u91cf\uff0c\u9700\u8981\u8bbe\u7f6e\u4ee5\u4e0b\u5c5e\u6027\u3002<\/p>\n<pre>bootRun {\r\n    \/\/classpath configurations.testRuntime\r\n   systemProperties = System.properties\r\n}<\/pre>\n<p>3\u3001\u518d\u67e5\u770b\u4e0b\u600e\u4e48\u4f7f\u7528bootrun\u4eceproperties\u4e2d\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u3002<\/p>\n<p>\u5728junit\u4e2d\uff0c\u4f1a\u52a0\u8f7dlogback-test.properties\uff0c\u90a3\u80fd\u5728debug\u6a21\u5f0f\u4e2d\u4f7f\u7528\u5417\u3002\u53ea\u8981\u662fjunit\u90fd\u4f1a\u52a0\u8f7dtest resources\u3002<\/p>\n<p>\u90a3\u8fd9\u6837\u6765\u8bf4\uff0cdebug\u6a21\u5f0f\u672c\u5c31\u4e0d\u5e94\u8be5\u53bb\u52a0\u8f7dtest resources\uff0c\u4e5f\u662f\u6b63\u786e\u7684\u5904\u7406\u65b9\u5f0f\uff0c\u53ea\u6709test\u6a21\u5f0f\u65f6\u624d\u9700\u8981\u53bb\u52a0\u8f7d test resources\u3002<\/p>\n<p>\u8d39\u8fd9\u725b\u9f3b\u5b50\u52b2\u5e72\u5565\uff0c\u76f4\u63a5\u5199\u4e2a\u811a\u672c\uff0c\u53c2\u6570\u901a\u8fc7\u73af\u5883\u53d8\u91cf\u4f20\u9012\u4e0d\u5c31OK\u5566\u3002<\/p>\n<p>\u6216\u8005bootrun\u65f6\uff0c\u80fd\u5224\u65ad\u51fa\u6765\uff0c\u4e3b\u52a8\u53bb\u52a0\u8f7d\u4e00\u4e2a\u4e5f\u53ef\u4ee5\u3002<\/p>\n<p>spring boot \u52a0\u8f7d\u987a\u5e8f<\/p>\n<p>https:\/\/docs.spring.io\/spring-boot\/docs\/current\/reference\/html\/boot-features-external-config.html<\/p>\n<pre class=\"brush: bash; gutter: true\">Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:\r\n\r\n    Devtools global settings properties on your home directory (~\/.spring-boot-devtools.properties when devtools is active).\r\n    @TestPropertySource annotations on your tests.\r\n    @SpringBootTest#properties annotation attribute on your tests.\r\n    Command line arguments.\r\n    Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).\r\n    ServletConfig init parameters.\r\n    ServletContext init parameters.\r\n    JNDI attributes from java:comp\/env.\r\n    Java System properties (System.getProperties()).\r\n    OS environment variables.\r\n    A RandomValuePropertySource that has properties only in random.*.\r\n    Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).\r\n    Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).\r\n    Application properties outside of your packaged jar (application.properties and YAML variants).\r\n    Application properties packaged inside your jar (application.properties and YAML variants).\r\n    @PropertySource annotations on your @Configuration classes.\r\n    Default properties (specified by setting SpringApplication.setDefaultProperties).<\/pre>\n<p>\u8fd8\u5f97\u770b\u4e0bspring loaded\u4e0ekotlin\u7684\u7ed3\u5408\u4e2d\u3002<\/p>\n<p>\u8fd8\u5f97\u770b\u4e0bdevtool\u7684\u4f5c\u7528\u3002<\/p>\n<p>\u4e0b\u9762\u5f00\u59cb\u6d4b\u8bd5\u4e0b devtool \u4e0e springloaded\uff0c\u4e3b\u8981\u5c31\u662fcontroller\uff0c\u63a5\u53e3\u589e\u52a0\uff0c\u4fee\u6539\uff0c\u5220\u9664\u662f\u5426\u80fd\u7acb\u5373\u751f\u6548\u3002<\/p>\n<p>\u8bb0\u5f97\u4e4b\u524d\u7528eclipse+java app+springloaded\uff0c\u589e\u52a0\u65b9\u6cd5\u90fd\u652f\u6301\u3002<\/p>\n<p>\u5728\u5916\u90e8\u8fd0\u884cgradlew.bat bootrun<\/p>\n<p>1\u3001\u4f7f\u7528devtool<\/p>\n<p>2\u3001\u4f7f\u7528springloaded\uff0c\u4ece\u73af\u5883\u53d8\u91cf\u4f20\u5165<\/p>\n<p>3\u3001\u4f7f\u7528springloaded\uff0c\u4ecebuild.gradle\u4e2d\u914d\u7f6e<\/p>\n<p>\u76f4\u63a5\u8fd0\u884cApp.kt<\/p>\n<p>1\u3001\u597d\u50cf\u53ea\u80fd\u4f7f\u7528\u73af\u5883\u53d8\u91cf\u6765\u914d\u7f6e\u4e86\u5427\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u65e9\u5efa\u8fc7\u4e00\u4e2akotlin\u5de5\u7a0b\uff0c\u5c1d\u8bd5\u4e86\u4e0bkotlin\u7684\u8bed\u6cd5\u3002 \u4eca\u5929\u51c6\u5907\u4e3a\u6211\u7684app &hellip;<\/p>\n<p class=\"read-more\"><a href=\"https:\/\/blog.zhukunqian.com\/?p=2186\">\u7ee7\u7eed\u9605\u8bfb &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[24],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/2186"}],"collection":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2186"}],"version-history":[{"count":18,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/2186\/revisions"}],"predecessor-version":[{"id":2211,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=\/wp\/v2\/posts\/2186\/revisions\/2211"}],"wp:attachment":[{"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.zhukunqian.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}