{"id":86945,"date":"2020-04-22T23:31:04","date_gmt":"2020-04-22T15:31:04","guid":{"rendered":"http:\/\/4563.org\/?p=86945"},"modified":"2020-04-22T23:31:04","modified_gmt":"2020-04-22T15:31:04","slug":"springboot%ef%bc%8c%e6%9c%89%e4%b8%aa%e8%87%aa%e5%8a%a8%e9%85%8d%e7%bd%ae%e7%9a%84%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=86945","title":{"rendered":"springboot\uff0c\u6709\u4e2a\u81ea\u52a8\u914d\u7f6e\u7684\u95ee\u9898"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  springboot\uff0c\u6709\u4e2a\u81ea\u52a8\u914d\u7f6e\u7684\u95ee\u9898               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : luxinfl <\/span>  <span><i><\/i> 10<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>@Bean @ConfigurationProperties(prefix = &#8220;ds2&#8221;) public DataSource dataSource(){ return DataSourceBuilder.create().build(); }<\/p>\n<h2>&#8212;&#8212;&#8212;&#8211;properties \u6587\u4ef6&#8212;&#8212;&#8212;&#8212;&#8211; ds2.jdbcUrl=xxx ds2.username=111 ds2.password=222 ds2.driverClassName=com.mysql.cj.jdbc.Driver<\/h2>\n<p>\u4e3a\u4ec0\u4e48\u751f\u6210\u6570\u636e\u6e90\u7684\u65f6\u5019\uff0c\u6ca1\u6709\u81ea\u52a8\u628a\u914d\u7f6e\u6587\u4ef6\u7684\u53c2\u6570\u5e26\u8fdb\u53bb\u554a\uff1f\u662f\u6211\u54ea\u91cc\u8bbe\u7f6e\u9519\u4e86\u4e48\uff1f<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>18<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"1351280\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : luxinfl <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Bean<br \/> @ConfigurationProperties(prefix = &#8220;spring.datasource.hikari&#8221;)<br \/> public DataSource dataSource(){<br \/> return DataSourceBuilder.create().build();<br \/> }                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1351281\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : luxinfl <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             ds2.jdbcUrl=<br \/>ds2.username=feb-dev<br \/>ds2.password=feb-dev<br \/>ds2.driverClassName=com.mysql.cj.jdbc.Driver                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1351282\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : luxinfl <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @ConfigurationProperties(prefix = &#8220;ds2&#8221;) \u6ce8\u89e3\u662f\u8fd9\u4e2a\u3001\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1351283\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wangyanrui <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             Binding is either performed by calling setters on the annotated class or, if {@link ConstructorBinding @ConstructorBinding} is in use, by binding to the constructor parameters.<br \/>\u6765\u81ea\u4e8e \u6e90\u7801\u91cc\u5907\u6ce8\u7684\u4e00\u90e8\u5206<\/p>\n<p>\u4f60\u7684 datasource \u91cc\u9762\uff0c\u6709 ds2 \u8fd9\u4e2a\u5c5e\u6027\u5417\uff1f                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1351284\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : luxinfl <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wangyanrui \u90a3\u5982\u679c\u8981\u7ed1\u5b9a hikari \u7684\u53c2\u6570\uff0c\u90a3\u5e94\u8be5\u600e\u4e48\u5199\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1351285\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : liyanggyang <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5927\u54e5\uff0c\u9996\u5148 \u542f\u52a8\u7c7b\u9700\u8981 @EnableConfigurationProperties;\u5176\u6b21\u81ea\u52a8\u6ce8\u89e3\u662f\u89e3\u6790 @Bean \u7684\u5165\u53c2\u7c7b\uff1b\u6700\u540e\uff0c\u4e5f\u662f\u6839\u636e\u6210\u5458\u53d8\u91cf\u7684 setter \uff08 DataSourceBuilder \u7684 username \u8fd9\u4e9b\uff0c\u4e0d\u662f DataSourceBuilder \u7684\u6210\u5458\u53d8\u91cf\uff09\uff1b<\/p>\n<p>&#8212;&#8212;-\u6211\u89c9\u5f97\u53ef\u4ee5\u8fd9\u6837&#8212;-<br \/>1. \u542f\u52a8\u7c7b<br \/>@EnableConfigurationProperties({<br \/> DataProperties.class,<br \/>})<br \/>2. \u914d\u7f6e\u7c7b\uff1a<br \/>@Setter<br \/>@Getter<br \/>@AllArgsConstructor<br \/>@NoArgsConstructor<br \/>@Builder<br \/>@ConfigurationProperties(prefix = &#8220;ds2&#8221;)<br \/>public class DataProperties {<br \/> private String username;<br \/>\u3002\u3002\u3002<br \/>}<br \/>3. \u6ce8\u518c bean<br \/>@Bean <br \/>public DataSource dataSource(DataProperties dataProperties){ <br \/>return DataSourceBuilder.create().username(dataProperties.getUserName).build(); }                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1351286\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Resource <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             DataSourceProperties \u6709 jdbcUrl \u8fd9\u4e2a\u5c5e\u6027\u5417\uff0c\u6211\u548b\u8bb0\u5f97\u662f url                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1351287\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : luxinfl <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @liyanggyang \u6211\u89c9\u7684 setter \u5e94\u8be5\u4e0d\u9700\u8981\uff0c\u56e0\u4e3a\u8fd9\u6837\u5199\u6211\u53ef\u4ee5\u83b7\u53d6\u5230\u914d\u7f6e\u4fe1\u606f<br \/>@Configuration<br \/>@PropertySource(value= &#8220;classpath:application-ds.yml&#8221;,factory = PropertySourceConfig.class)<br \/>@EnableConfigurationProperties<br \/>public class TransactionConfig {<\/p>\n<p> @Bean<br \/> @ConfigurationProperties(prefix = &#8220;ds2&#8221;)<br \/> public DataSource dataSource() {<br \/> return DataSourceBuilder.create().build();<br \/> }<\/p>\n<p>}                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1351288\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : luxinfl <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Resource hikari \u7684 url \u53d8\u91cf\u4e0d\u592a\u4e00\u6837                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1351289\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : luxinfl <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wangyanrui ds2 \u524d\u9762\u52a0\u4e86 prefix\uff0c\u53ea\u662f\u914d\u7f6e\u7684\u524d\u7f00                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1351290\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wangyanrui <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u6709\u660e\u767d\u4e3b\u5230\u5e95\u8981\u8868\u8fbe\u7684\u662f\u4ec0\u4e48\u610f\u601d\uff08\u624b\u52a8\u6c9f\u901a\uff09                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1351291\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wangyanrui <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6ca1\u6709\u660e\u767d\u4e3b\u5230\u5e95\u8981\u8868\u8fbe\u7684\u662f\u4ec0\u4e48\u610f\u601d\uff08\u624b\u52a8\u72d7\u5934\uff09                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1351292\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wangyanrui <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u611f\u89c9\u4e3b\u53ef\u80fd\u5bf9 SpringBoot \u7684\u81ea\u52a8\u914d\u7f6e\u7684\u539f\u7406\u4e0d\u592a\u660e\u767d\uff0c\u4e91\u91cc\u96fe\u91cc\u7684<br \/>\u5efa\u8bae\u5148\u4e86\u89e3\u4e00\u4e9b\u57fa\u672c\u6982\u5ff5<br \/>\u8bf4\u9519\u4e86\u52ff\u9524                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1351293\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : luxinfl <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wangyanrui \u662f\u4e0d\u592a\u61c2\uff0c\u4f46\u662f\u4e3a\u5565\u6211\u6362\u4e86\u4e2a\u5199\u6cd5\u5c31\u80fd\u8bfb\u53d6\u5230\u4e86\u5462\uff1f<\/p>\n<p>@Configuration<br \/>@PropertySource(value= &#8220;classpath:application-ds.yml&#8221;,factory = PropertySourceConfig.class)<br \/>@EnableConfigurationProperties<br \/>public class TransactionConfig {<\/p>\n<p>@Bean<br \/>@ConfigurationProperties(prefix = &#8220;ds2&#8221;)<br \/>public DataSource dataSource() {<br \/>return DataSourceBuilder.create().build();<br \/>}<\/p>\n<p>}                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1351294\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : chendy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             DataSource \u4e0d\u5403\u8fd9\u4e9b\u914d\u7f6e\uff0c\u5e94\u8be5\u8fd4\u56de DataSourceProperties \u6216\u8005 HIkari \u7684\u914d\u7f6e\u7c7b\uff0c\u914d\u7f6e\u53c2\u6570\u624d\u80fd\u52a0\u4e0a\u53bb<br \/>\u7136\u540e\u518d\u6ce8\u5165\u7ed9\u76f8\u5173\u7684 bean                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1351295\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : luxinfl <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @chendy \u6211\u8fd8\u662f\u770b\u770b\u914d\u7f6e\u662f\u600e\u4e48\u52a0\u8f7d\u7684\u5427\u3002\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1351296\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Jrue0011 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u81ea\u5df1\u6d4b\u4e86\u4e0b\uff0c\u5355\u6570\u636e\u6e90\u7528\u4f60\u4e3b\u8d34\u7684\u5199\u6cd5\u6ca1\u4ec0\u4e48\u95ee\u9898\uff0c\u53ef\u80fd\u662f\u591a\u6570\u636e\u6e90\u7ed3\u5408\u81ea\u52a8\u914d\u7f6e\u540e\u7684\u95ee\u9898                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"1351297\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : luxinfl <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u53c8\u8bd5\u4e86\u4e0b\u3002 @ConfigurationProperties \u53ef\u4ee5\u76f4\u63a5\u8bfb\u5230 application.properties \u6216\u8005 application.yml \u914d\u7f6e\u6587\u4ef6\u3002\u4f46\u662f\u5982\u679c\u6570\u636e\u6e90\u914d\u7f6e\u5199\u5728\u81ea\u5b9a\u4e49\u6587\u4ef6\u7684\u65f6\u5019\uff0c\u8981\u4e48\u7528 @PropertySource \u8bfb\u53d6\u81ea\u5b9a\u4e49\u914d\u7f6e\u6587\u4ef6\uff0c\u8981\u4e48\u5728\u4e3b\u914d\u7f6e\u6587\u4ef6\u914d\u7f6e spring.profiles.include..\u8c22\u8c22\u5927\u4f6c\u4eec\u63d0\u4f9b\u610f\u89c1<br \/>@Jrue0011 <br \/>@liyanggyang <br \/>@wangyanrui                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>springboot\uff0c\u6709\u4e2a\u81ea\u52a8\u914d\u7f6e&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/86945"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=86945"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/86945\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=86945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=86945"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=86945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}