{"id":84,"date":"2018-04-11T13:33:43","date_gmt":"2018-04-11T05:33:43","guid":{"rendered":"http:\/\/chenhaitao.me\/?p=84"},"modified":"2018-04-11T13:33:43","modified_gmt":"2018-04-11T05:33:43","slug":"spring-boot%e4%bd%bf%e7%94%a8%ef%bc%9a%e5%ba%94%e7%94%a8%e5%90%af%e5%8a%a8%e5%90%8e%e5%81%9a%e7%9a%84%e4%b8%80%e4%ba%9b%e4%ba%8b","status":"publish","type":"post","link":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/2018\/04\/11\/spring-boot%e4%bd%bf%e7%94%a8%ef%bc%9a%e5%ba%94%e7%94%a8%e5%90%af%e5%8a%a8%e5%90%8e%e5%81%9a%e7%9a%84%e4%b8%80%e4%ba%9b%e4%ba%8b\/","title":{"rendered":"Spring Boot\u4f7f\u7528\uff1a\u5e94\u7528\u542f\u52a8\u540e\u505a\u7684\u4e00\u4e9b\u4e8b"},"content":{"rendered":"<p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5728\u5e94\u7528\u542f\u52a8\u6216\u8005\u91cd\u542f\u540e\u505a\u4e00\u4e9b\u5de5\u4f5c\uff0c\u6bd4\u5982\u5220\u9664\u4e00\u4e9b\u4e34\u65f6\u6587\u4ef6\u6216\u8005Redis\u4e2d\u7684\u7f13\u5b58\uff0c\u52a0\u8f7d\u4e00\u4e9b\u9891\u7e41\u4f7f\u7528\u7684List\u7b49\u3002\u8fd9\u5728\u6ca1\u6709\u4f7f\u7528Spring Boot\u4e4b\u524d\uff0c\u5728Spring\u6846\u67b6\u4e0b\u662f\u901a\u8fc7ApplicationListener\u76d1\u542c\u5668\u6765\u5b9e\u73b0\u7684\uff0c\u6216\u8005\u662fConfigServlet\u3002<br \/>\n\u5728Spring Boot\u4e0b\u4e5f\u6709\u4e00\u4e9b\u529e\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528CommandLineRunner\u548cApplicationRunner\u63a5\u53e3\u6765\u5b9e\u73b0\u3002<\/p>\n<p>\u5728\u8fd9\u4e24\u4e2a\u63a5\u53e3\u4e2d\u90fd\u6709run()\u65b9\u6cd5\uff0c\u5728Spring Boot\u7a0b\u5e8f\u542f\u52a8\u540e\u4f1a\u68c0\u6d4b\u7a0b\u5e8f\u4e2d\u662f\u5426\u6709CommandLineRunner\u548cApplicationRunner\u63a5\u53e3\u7684\u5b9e\u4f8b\uff0c\u5982\u679c\u5b58\u5728\uff0c\u5219\u4f1a\u6267\u884c\u5bf9\u5e94\u5b9e\u73b0\u7c7b\u4e2d\u7684run()\u65b9\u6cd5\uff0c\u800c\u4e14\u53ea\u6267\u884c\u4e00\u6b21\u3002<\/p>\n<p>    Spring Boot\u7a0b\u5e8f\u542f\u52a8\u8fc7\u7a0b\u4e2d\u4f1a\u5b9e\u4f8b\u5316ApplicationContext\uff0c\u7136\u540e\u521b\u5efa\u6240\u6709\u7684Bean\u3002<\/p>\n<p>\u5982\u679c\u5b58\u5728\u591a\u4e2aCommandLineRunner\u548cApplicationRunner\u63a5\u53e3\u7684\u5b9e\u4f8b\uff0c\u90a3\u4e48\u5b83\u4eec\u7684\u6267\u884c\u987a\u5e8f\u53ef\u4ee5\u901a\u8fc7@Order\u6ce8\u89e3\u548cOrdered\u63a5\u53e3\u6765\u6807\u8bc6\uff0c\u6570\u5b57\u8d8a\u5c0f\uff0c\u4f18\u5148\u7ea7\u8d8a\u9ad8\u3002<\/p>\n<p>\u770b\u5982\u4e0b\u5b9e\u4f8b\uff1a<\/p>\n<p>TaskRunner\u7c7b\uff1a<\/p>\n<p>    public class TaskRunner implements ApplicationRunner, Ordered {<br \/>\n        private static final Logger logger = LoggerFactory.getLogger(TaskRunner.class);<\/p>\n<p>        @Override<br \/>\n        public int getOrder() {<br \/>\n            return 2;<br \/>\n        }<\/p>\n<p>        @Override<br \/>\n        public void run(ApplicationArguments args) throws Exception {<br \/>\n            logger.info(&#8220;task runner&#8221;);<br \/>\n        }<br \/>\n    }<\/p>\n<p>StartupRunner\u7c7b\uff1a<\/p>\n<p>    @Order(1)<br \/>\n    public class StartupRunner implements CommandLineRunner {<br \/>\n        private static final Logger logger = LoggerFactory.getLogger(StartupRunner.class);<\/p>\n<p>        @Autowired<br \/>\n        @Qualifier(&#8220;primaryDataSource&#8221;)<br \/>\n        private DataSource primaryDataSource;<\/p>\n<p>        @Autowired<br \/>\n        @Qualifier(&#8220;secondaryDataSource&#8221;)<br \/>\n        private DataSource secondDataSource;<\/p>\n<p>        @Override<br \/>\n        public void run(String&#8230; args) throws Exception {<br \/>\n            logger.info(&#8220;startup runner&#8221;);<br \/>\n            System.out.print(primaryDataSource.getConnection().getMetaData().getURL());<br \/>\n            System.out.print(secondDataSource.getConnection().getMetaData().getURL());<br \/>\n        }<br \/>\n    }<\/p>\n<p>\u7136\u540e\u5728Application\u4e3b\u7c7b\u4e2d\u5b9a\u4e49\u8fd9\u4e24\u4e2abean\uff0c\u7531\u4e8e@SpringBootApplication\u6ce8\u89e3\u4e2d\u5305\u542b@Configuration\u6ce8\u89e3\uff0c\u6240\u4ee5@Bean\u6ce8\u89e3\u5728\u5176\u4e2d\u4e5f\u4f1a\u8d77\u4f5c\u7528\uff0c\u5982\u4e0b\uff1a<\/p>\n<p>    @SpringBootApplication<br \/>\n    public class SpringBootTemplateApplication {<\/p>\n<p>        public static void main(String[] args) {<br \/>\n            SpringApplication.run(SpringBootTemplateApplication.class, args);<br \/>\n        }<\/p>\n<p>        @Bean<br \/>\n        public StartupRunner startupRunner(){<br \/>\n            return new StartupRunner();<br \/>\n        }<\/p>\n<p>        @Bean<br \/>\n        public TaskRunner taskRunner(){<br \/>\n            return new TaskRunner();<br \/>\n        }<br \/>\n    }<\/p>\n<p>\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n<p>\u53ef\u4ee5\u770b\u51fa\u7531\u4e8eStartupRunner\u7c7b\u4f18\u5148\u7ea7\u8f83\u9ad8\u6240\u4ee5\u5148\u8fd0\u884c\uff0cTaskRunner\u540e\u8fd0\u884c\u3002<\/p>\n<p>\u5173\u4e8eCommandLineRunner\u548cApplicationRunner\u7684\u533a\u522b\uff0c\u5728\u5b98\u65b9\u6587\u6863\u4e2d\u662f\u8fd9\u4e48\u8bf4\u7684\uff1a<\/p>\n<p>    Interface used to indicate that a bean should run when it is contained within<br \/>\n    a SpringApplication. Multiple CommandLineRunner beans can be defined<br \/>\n    within the same application context and can be ordered using the Ordered<br \/>\n    interface or @Order annotation.<br \/>\n    If you need access to ApplicationArguments instead of the raw String array consider using ApplicationRunner.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u65f6\u5019\u6211\u4eec\u9700\u8981\u5728\u5e94\u7528\u542f\u52a8\u6216\u8005\u91cd\u542f\u540e\u505a\u4e00\u4e9b\u5de5\u4f5c\uff0c\u6bd4\u5982\u5220\u9664\u4e00\u4e9b\u4e34\u65f6\u6587\u4ef6\u6216\u8005Redis\u4e2d\u7684\u7f13\u5b58\uff0c\u52a0\u8f7d\u4e00\u4e9b\u9891\u7e41\u4f7f\u7528\u7684List\u7b49\u3002\u8fd9\u5728\u6ca1\u6709\u4f7f\u7528Spring Boot\u4e4b\u524d\uff0c&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/chenhaitao.com\/.wp-toolkit_I\/2018\/04\/11\/spring-boot%e4%bd%bf%e7%94%a8%ef%bc%9a%e5%ba%94%e7%94%a8%e5%90%af%e5%8a%a8%e5%90%8e%e5%81%9a%e7%9a%84%e4%b8%80%e4%ba%9b%e4%ba%8b\/\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[30],"tags":[31],"class_list":["post-84","post","type-post","status-publish","format-standard","hentry","category-30","tag-srping-boot"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9HWWB-1m","jetpack-related-posts":[{"id":23,"url":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/2018\/03\/06\/springboot-schedule-%e9%85%8d%e7%bd%ae\/","url_meta":{"origin":84,"position":0},"title":"SpringBoot Schedule \u914d\u7f6e","author":"\u6770\u514b\u9648","date":"2018\u5e743\u67086\u65e5","format":false,"excerpt":"1. \u5b9a\u65f6\u4efb\u52a1\u5b9e\u73b0\u65b9\u5f0f \u5b9a\u65f6\u4efb\u52a1\u5b9e\u73b0\u65b9\u5f0f\uff1a Java\u81ea\u5e26\u7684java.util.Timer\u7c7b\uff0c\u8fd9\u4e2a\u7c7b\u5141\u2026","rel":"","context":"\u5728\u201c\u6280\u672f\u201d\u4e2d","block_context":{"text":"\u6280\u672f","link":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/category\/%e6%8a%80%e6%9c%af\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":32,"url":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/2018\/03\/09\/spring-%e7%9a%84scheduled%e6%b3%a8%e8%a7%a3%e5%ae%9e%e7%8e%b0%e5%ae%9a%e6%97%b6%e4%bb%bb%e5%8a%a1%e6%89%a7%e8%a1%8c%e5%92%8c%e8%b0%83%e5%ba%a6\/","url_meta":{"origin":84,"position":1},"title":"Spring \u7684@Scheduled\u6ce8\u89e3\u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1\u6267\u884c\u548c\u8c03\u5ea6","author":"\u6770\u514b\u9648","date":"2018\u5e743\u67089\u65e5","format":false,"excerpt":"Spring \u7684@Scheduled\u6ce8\u89e3\u5b9e\u73b0\u5b9a\u65f6\u4efb\u52a1\u6267\u884c\u548c\u8c03\u5ea6 \u9996\u5148\u8981\u914d\u7f6e\u6211\u4eec\u7684spring.xml\u2026","rel":"","context":"\u5728\u201c\u6280\u672f\u201d\u4e2d","block_context":{"text":"\u6280\u672f","link":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/category\/%e6%8a%80%e6%9c%af\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5,"url":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/2018\/03\/03\/lombok%e7%9a%84%e4%bd%bf%e7%94%a8\/","url_meta":{"origin":84,"position":2},"title":"lombok\u7684\u4f7f\u7528","author":"\u6770\u514b\u9648","date":"2018\u5e743\u67083\u65e5","format":false,"excerpt":"\u80cc\u666f \u5728\u65e5\u5e38\u7684\u5f00\u53d1\u4e2d,\u6709\u5f88\u591a\u5de5\u4f5c\u90fd\u662f\u91cd\u590d\u7684,\u6bd4\u5982: - \u6211\u4eec\u4f1a\u5b9a\u4e49\u5f88\u591a\u7684domain\u5b9e\u4f53\u7c7b,\u9700\u8981\u7ed9\u6bcf\u2026","rel":"","context":"\u5728\u201c\u6280\u672f\u201d\u4e2d","block_context":{"text":"\u6280\u672f","link":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/category\/%e6%8a%80%e6%9c%af\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":38,"url":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/2018\/03\/10\/springboot%e6%95%b4%e5%90%88mybatis\/","url_meta":{"origin":84,"position":3},"title":"SpringBoot\u6574\u5408Mybatis","author":"\u6770\u514b\u9648","date":"2018\u5e743\u670810\u65e5","format":false,"excerpt":"\u65b9\u5f0f\u4e00\uff1a\u4f7f\u7528Annotation\u81ea\u52a8\u5316\u96c6\u6210 \u5f53\u6211\u6309\u7167\u7f51\u4e0a\u7684\u6559\u7a0b\u914d\u7f6e\u540e\uff0c\u5f88\u7b80\u5355\u561b\uff0c\u57fa\u672c\u4e0a\u5565\u4e5f\u4e0d\u7528\u5e72\uff0c\u914d\u2026","rel":"","context":"\u5728\u201c\u6280\u672f\u201d\u4e2d","block_context":{"text":"\u6280\u672f","link":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/category\/%e6%8a%80%e6%9c%af\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":107,"url":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/2018\/05\/02\/windows-%e4%b8%8b%e4%bf%ae%e6%94%b9tomcat-jvm%e5%8f%82%e6%95%b0\/","url_meta":{"origin":84,"position":4},"title":"Windows \u4e0b\u4fee\u6539Tomcat jvm\u53c2\u6570","author":"\u6770\u514b\u9648","date":"2018\u5e745\u67082\u65e5","format":false,"excerpt":"\u4e00\u3001\u8bbe\u7f6eWindows\u670d\u52a1\u81ea\u52a8\u542f\u52a8\u65b9\u5f0f\u4fee\u6539 \u4fee\u6539\u6ce8\u518c\u8868\u6216\u8005\u4fee\u6539\u8fd0\u884ctomcatW.exe\u51fa\u6765\u7684\u201cJa\u2026","rel":"","context":"\u5728\u201c\u8fd0\u7ef4\u201d\u4e2d","block_context":{"text":"\u8fd0\u7ef4","link":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/category\/%e8%bf%90%e7%bb%b4\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":152,"url":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/2018\/10\/06\/java-web%e6%9c%8d%e5%8a%a1%e5%99%a8%e5%ae%9e%e7%8e%b0%e8%b7%a8%e5%9f%9f%e8%ae%bf%e9%97%ae\/","url_meta":{"origin":84,"position":5},"title":"java web\u670d\u52a1\u5668\u5b9e\u73b0\u8de8\u57df\u8bbf\u95ee","author":"\u6770\u514b\u9648","date":"2018\u5e7410\u67086\u65e5","format":false,"excerpt":"\u4e00\u3001CORS\u6982\u8ff0 \u8de8\u6e90\u8d44\u6e90\u5171\u4eab\u6807\u51c6\u901a\u8fc7\u65b0\u589e\u4e00\u7cfb\u5217 HTTP \u5934\uff0c\u8ba9\u670d\u52a1\u5668\u80fd\u58f0\u660e\u90a3\u4e9b\u6765\u6e90\u53ef\u4ee5\u901a\u8fc7\u6d4f\u89c8\u5668\u2026","rel":"","context":"\u5728\u201c\u6280\u672f\u201d\u4e2d","block_context":{"text":"\u6280\u672f","link":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/category\/%e6%8a%80%e6%9c%af\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/posts\/84","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/comments?post=84"}],"version-history":[{"count":1,"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/posts\/84\/revisions"}],"predecessor-version":[{"id":85,"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/posts\/84\/revisions\/85"}],"wp:attachment":[{"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/media?parent=84"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/categories?post=84"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chenhaitao.com\/.wp-toolkit_I\/wp-json\/wp\/v2\/tags?post=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}