{"id":77,"date":"2018-03-05T10:54:57","date_gmt":"2019-03-05T02:54:57","guid":{"rendered":"http:\/\/www.anthonylee.cn\/myblog\/?p=77"},"modified":"2019-03-05T11:58:12","modified_gmt":"2019-03-05T03:58:12","slug":"spring-boot-%e5%b8%b8%e7%94%a8%e7%9a%84-25-%e4%b8%aa%e6%b3%a8%e8%a7%a3","status":"publish","type":"post","link":"http:\/\/www.anthonylee.cn\/myblog\/?p=77","title":{"rendered":"Spring Boot \u5e38\u7528\u7684 25 \u4e2a\u6ce8\u89e3"},"content":{"rendered":"<p><strong>1\u3001@SpringBootApplication<\/strong><\/p>\n<p>\u8fd9\u662f Spring Boot \u6700\u6700\u6700\u6838\u5fc3\u7684\u6ce8\u89e3\uff0c\u7528\u5728 Spring Boot \u4e3b\u7c7b\u4e0a\uff0c\u6807\u8bc6\u8fd9\u662f\u4e00\u4e2a Spring Boot \u5e94\u7528\uff0c\u7528\u6765\u5f00\u542f Spring Boot \u7684\u5404\u9879\u80fd\u529b\u3002<\/p>\n<p>\u5176\u5b9e\u8fd9\u4e2a\u6ce8\u89e3\u5c31\u662f @SpringBootConfiguration\u3001@EnableAutoConfiguration\u3001@ComponentScan \u8fd9\u4e09\u4e2a\u6ce8\u89e3\u7684\u7ec4\u5408\uff0c\u4e5f\u53ef\u4ee5\u7528\u8fd9\u4e09\u4e2a\u6ce8\u89e3\u6765\u4ee3\u66ff @SpringBootApplication \u6ce8\u89e3\u3002<\/p>\n<p><strong>2\u3001@EnableAutoConfiguration<\/strong><\/p>\n<p>\u5141\u8bb8 Spring Boot \u81ea\u52a8\u914d\u7f6e\u6ce8\u89e3\uff0c\u5f00\u542f\u8fd9\u4e2a\u6ce8\u89e3\u4e4b\u540e\uff0cSpring Boot \u5c31\u80fd\u6839\u636e\u5f53\u524d\u7c7b\u8def\u5f84\u4e0b\u7684\u5305\u6216\u8005\u7c7b\u6765\u914d\u7f6e Spring Bean\u3002<\/p>\n<p>\u5982\uff1a\u5f53\u524d\u7c7b\u8def\u5f84\u4e0b\u6709 Mybatis \u8fd9\u4e2a JAR \u5305\uff0cMybatisAutoConfiguration \u6ce8\u89e3\u5c31\u80fd\u6839\u636e\u76f8\u5173\u53c2\u6570\u6765\u914d\u7f6e Mybatis \u7684\u5404\u4e2a Spring Bean\u3002<\/p>\n<p><strong>3\u3001@Configuration<\/strong><\/p>\n<p>\u8fd9\u662f Spring 3.0 \u6dfb\u52a0\u7684\u4e00\u4e2a\u6ce8\u89e3\uff0c\u7528\u6765\u4ee3\u66ff applicationContext.xml \u914d\u7f6e\u6587\u4ef6\uff0c\u6240\u6709\u8fd9\u4e2a\u914d\u7f6e\u6587\u4ef6\u91cc\u9762\u80fd\u505a\u5230\u7684\u4e8b\u60c5\u90fd\u53ef\u4ee5\u901a\u8fc7\u8fd9\u4e2a\u6ce8\u89e3\u6240\u5728\u7c7b\u6765\u8fdb\u884c\u6ce8\u518c\u3002<\/p>\n<p><strong>4\u3001@SpringBootConfiguration<\/strong><\/p>\n<p>\u8fd9\u4e2a\u6ce8\u89e3\u5c31\u662f @Configuration \u6ce8\u89e3\u7684\u53d8\u4f53\uff0c\u53ea\u662f\u7528\u6765\u4fee\u9970\u662f Spring Boot \u914d\u7f6e\u800c\u5df2\uff0c\u6216\u8005\u53ef\u5229\u4e8e Spring Boot \u540e\u7eed\u7684\u6269\u5c55\u3002<\/p>\n<p><strong>5\u3001@ComponentScan<\/strong><\/p>\n<p>\u8fd9\u662f Spring 3.1 \u6dfb\u52a0\u7684\u4e00\u4e2a\u6ce8\u89e3\uff0c\u7528\u6765\u4ee3\u66ff\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684 component-scan \u914d\u7f6e\uff0c\u5f00\u542f\u7ec4\u4ef6\u626b\u63cf\uff0c\u5373\u81ea\u52a8\u626b\u63cf\u5305\u8def\u5f84\u4e0b\u7684 @Component \u6ce8\u89e3\u8fdb\u884c\u6ce8\u518c bean \u5b9e\u4f8b\u5230 context \u4e2d\u3002<\/p>\n<p><strong>6\u3001@Conditional<\/strong><\/p>\n<p>\u8fd9\u662f Spring 4.0 \u6dfb\u52a0\u7684\u65b0\u6ce8\u89e3\uff0c\u7528\u6765\u6807\u8bc6\u4e00\u4e2a Spring Bean \u6216\u8005 Configuration \u914d\u7f6e\u6587\u4ef6\uff0c\u5f53\u6ee1\u8db3\u6307\u5b9a\u7684\u6761\u4ef6\u624d\u5f00\u542f\u914d\u7f6e\u3002<\/p>\n<p><strong>7\u3001@ConditionalOnBean<\/strong><\/p>\n<p>\u7ec4\u5408 @Conditional \u6ce8\u89e3\uff0c\u5f53\u5bb9\u5668\u4e2d\u6709\u6307\u5b9a\u7684 Bean \u624d\u5f00\u542f\u914d\u7f6e\u3002<\/p>\n<p><strong>8\u3001@ConditionalOnMissingBean<\/strong><\/p>\n<p>\u7ec4\u5408 @Conditional \u6ce8\u89e3\uff0c\u548c @ConditionalOnBean \u6ce8\u89e3\u76f8\u53cd\uff0c\u5f53\u5bb9\u5668\u4e2d\u6ca1\u6709\u6307\u5b9a\u7684 Bean \u624d\u5f00\u542f\u914d\u7f6e\u3002<\/p>\n<p><strong>9\u3001@ConditionalOnClass<\/strong><\/p>\n<p>\u7ec4\u5408 @Conditional \u6ce8\u89e3\uff0c\u5f53\u5bb9\u5668\u4e2d\u6709\u6307\u5b9a\u7684 Class \u624d\u5f00\u542f\u914d\u7f6e\u3002<\/p>\n<p><strong>10\u3001@ConditionalOnMissingClass<\/strong><\/p>\n<p>\u7ec4\u5408 @Conditional \u6ce8\u89e3\uff0c\u548c @ConditionalOnMissingClass \u6ce8\u89e3\u76f8\u53cd\uff0c\u5f53\u5bb9\u5668\u4e2d\u6ca1\u6709\u6307\u5b9a\u7684 Class \u624d\u5f00\u542f\u914d\u7f6e\u3002<\/p>\n<p><strong>11\u3001@ConditionalOnWebApplication<\/strong><\/p>\n<p>\u7ec4\u5408 @Conditional \u6ce8\u89e3\uff0c\u5f53\u524d\u9879\u76ee\u7c7b\u578b\u662f WEB \u9879\u76ee\u624d\u5f00\u542f\u914d\u7f6e\u3002<\/p>\n<p><strong>12\u3001@ConditionalOnNotWebApplication<\/strong><\/p>\n<p>\u7ec4\u5408 @Conditional \u6ce8\u89e3\uff0c\u548c @ConditionalOnWebApplication \u6ce8\u89e3\u76f8\u53cd\uff0c\u5f53\u524d\u9879\u76ee\u7c7b\u578b\u4e0d\u662f WEB \u9879\u76ee\u624d\u5f00\u542f\u914d\u7f6e\u3002<\/p>\n<p><strong>13\u3001@ConditionalOnProperty<\/strong><\/p>\n<p>\u7ec4\u5408 @Conditional \u6ce8\u89e3\uff0c\u5f53\u6307\u5b9a\u7684\u5c5e\u6027\u6709\u6307\u5b9a\u7684\u503c\u65f6\u624d\u5f00\u542f\u914d\u7f6e\u3002<\/p>\n<p><strong>14\u3001@ConditionalOnExpression<\/strong><\/p>\n<p>\u7ec4\u5408 @Conditional \u6ce8\u89e3\uff0c\u5f53 SpEL \u8868\u8fbe\u5f0f\u4e3a true \u65f6\u624d\u5f00\u542f\u914d\u7f6e\u3002<\/p>\n<p><strong>15\u3001@ConditionalOnJava<\/strong><\/p>\n<p>\u7ec4\u5408 @Conditional \u6ce8\u89e3\uff0c\u5f53\u8fd0\u884c\u7684 Java JVM \u5728\u6307\u5b9a\u7684\u7248\u672c\u8303\u56f4\u65f6\u624d\u5f00\u542f\u914d\u7f6e\u3002<\/p>\n<p><strong>16\u3001@ConditionalOnResource<\/strong><\/p>\n<p>\u7ec4\u5408 @Conditional \u6ce8\u89e3\uff0c\u5f53\u7c7b\u8def\u5f84\u4e0b\u6709\u6307\u5b9a\u7684\u8d44\u6e90\u624d\u5f00\u542f\u914d\u7f6e\u3002<\/p>\n<p><strong>17\u3001@ConditionalOnJndi<\/strong><\/p>\n<p>\u7ec4\u5408 @Conditional \u6ce8\u89e3\uff0c\u5f53\u6307\u5b9a\u7684 JNDI \u5b58\u5728\u65f6\u624d\u5f00\u542f\u914d\u7f6e\u3002<\/p>\n<p><strong>18\u3001@ConditionalOnCloudPlatform<\/strong><\/p>\n<p>\u7ec4\u5408 @Conditional \u6ce8\u89e3\uff0c\u5f53\u6307\u5b9a\u7684\u4e91\u5e73\u53f0\u6fc0\u6d3b\u65f6\u624d\u5f00\u542f\u914d\u7f6e\u3002<\/p>\n<p><strong>19\u3001@ConditionalOnSingleCandidate<\/strong><\/p>\n<p>\u7ec4\u5408 @Conditional \u6ce8\u89e3\uff0c\u5f53\u6307\u5b9a\u7684 class \u5728\u5bb9\u5668\u4e2d\u53ea\u6709\u4e00\u4e2a Bean\uff0c\u6216\u8005\u540c\u65f6\u6709\u591a\u4e2a\u4f46\u4e3a\u9996\u9009\u65f6\u624d\u5f00\u542f\u914d\u7f6e\u3002<\/p>\n<p><strong>20\u3001@ConfigurationProperties<\/strong><\/p>\n<p>\u7528\u6765\u52a0\u8f7d\u989d\u5916\u7684\u914d\u7f6e\uff08\u5982 .properties \u6587\u4ef6\uff09\uff0c\u53ef\u7528\u5728 @Configuration \u6ce8\u89e3\u7c7b\uff0c\u6216\u8005 @Bean \u6ce8\u89e3\u65b9\u6cd5\u4e0a\u9762\u3002<\/p>\n<p><strong>21\u3001@EnableConfigurationProperties<\/strong><\/p>\n<p>\u4e00\u822c\u8981\u914d\u5408 @ConfigurationProperties \u6ce8\u89e3\u4f7f\u7528\uff0c\u7528\u6765\u5f00\u542f\u5bf9 @ConfigurationProperties \u6ce8\u89e3\u914d\u7f6e Bean \u7684\u652f\u6301\u3002<\/p>\n<p><strong>22\u3001@AutoConfigureAfter<\/strong><\/p>\n<p>\u7528\u5728\u81ea\u52a8\u914d\u7f6e\u7c7b\u4e0a\u9762\uff0c\u8868\u793a\u8be5\u81ea\u52a8\u914d\u7f6e\u7c7b\u9700\u8981\u5728\u53e6\u5916\u6307\u5b9a\u7684\u81ea\u52a8\u914d\u7f6e\u7c7b\u914d\u7f6e\u5b8c\u4e4b\u540e\u3002<\/p>\n<p>\u5982 Mybatis \u7684\u81ea\u52a8\u914d\u7f6e\u7c7b\uff0c\u9700\u8981\u5728\u6570\u636e\u6e90\u81ea\u52a8\u914d\u7f6e\u7c7b\u4e4b\u540e\u3002<\/p>\n<pre>@AutoConfigureAfter(DataSourceAutoConfiguration.class)\r\npublic class MybatisAutoConfiguration {<\/pre>\n<p><strong>23\u3001@AutoConfigureBefore<\/strong><\/p>\n<p>\u8fd9\u4e2a\u548c @AutoConfigureAfter \u6ce8\u89e3\u4f7f\u7528\u76f8\u53cd\uff0c\u8868\u793a\u8be5\u81ea\u52a8\u914d\u7f6e\u7c7b\u9700\u8981\u5728\u53e6\u5916\u6307\u5b9a\u7684\u81ea\u52a8\u914d\u7f6e\u7c7b\u914d\u7f6e\u4e4b\u524d\u3002<\/p>\n<p><strong>24\u3001@Import<\/strong><\/p>\n<p>\u8fd9\u662f Spring 3.0 \u6dfb\u52a0\u7684\u65b0\u6ce8\u89e3\uff0c\u7528\u6765\u5bfc\u5165\u4e00\u4e2a\u6216\u8005\u591a\u4e2a @Configuration \u6ce8\u89e3\u4fee\u9970\u7684\u7c7b\uff0c\u8fd9\u5728 Spring Boot \u91cc\u9762\u5e94\u7528\u5f88\u591a\u3002<\/p>\n<p><strong>25\u3001@ImportResource<\/strong><\/p>\n<p>\u8fd9\u662f Spring 3.0 \u6dfb\u52a0\u7684\u65b0\u6ce8\u89e3\uff0c\u7528\u6765\u5bfc\u5165\u4e00\u4e2a\u6216\u8005\u591a\u4e2a Spring \u914d\u7f6e\u6587\u4ef6\uff0c\u8fd9\u5bf9 Spring Boot \u517c\u5bb9\u8001\u9879\u76ee\u975e\u5e38\u6709\u7528\uff0c\u56e0\u4e3a\u6709\u4e9b\u914d\u7f6e\u65e0\u6cd5\u901a\u8fc7 Java Config \u7684\u5f62\u5f0f\u6765\u914d\u7f6e\u5c31\u53ea\u80fd\u7528\u8fd9\u4e2a\u6ce8\u89e3\u6765\u5bfc\u5165\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001@SpringBootApplication \u8fd9\u662f Spring Boot \u6700\u6700\u6700\u6838\u5fc3\u7684\u6ce8\u89e3\uff0c\u7528&hellip; <a href=\"http:\/\/www.anthonylee.cn\/myblog\/?p=77\" class=\"more-link read-more\" rel=\"bookmark\">\u7ee7\u7eed\u9605\u8bfb <span class=\"screen-reader-text\">Spring Boot \u5e38\u7528\u7684 25 \u4e2a\u6ce8\u89e3<\/span><i class=\"fa fa-arrow-right\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=\/wp\/v2\/posts\/77"}],"collection":[{"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=77"}],"version-history":[{"count":1,"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":78,"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=\/wp\/v2\/posts\/77\/revisions\/78"}],"wp:attachment":[{"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.anthonylee.cn\/myblog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}