日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

数据源配置和自动管理

發(fā)布時(shí)間:2025/4/16 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据源配置和自动管理 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

使用DriverManagerDataSource

修改yml

#端口 server:port: 8080 spring:#數(shù)據(jù)源datasource:url: jdbc:mysql://127.0.0.1:3306/dd?useUnicode=true&characterEncoding=utf8&useSSL=truedriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: root#配置數(shù)據(jù)源的類(lèi)型type: org.springframework.jdbc.datasource.DriverManagerDataSource

使用DBCP

修改pom.xml引入dbcp的包

<!--加入Dbcp的依賴(lài)--> <dependency><groupId>org.apache.commons</groupId><artifactId>commons-dbcp2</artifactId> </dependency> #端口 server:port: 8080 spring:#數(shù)據(jù)源datasource:url: jdbc:mysql://127.0.0.1:3306/dym11?useUnicode=true&characterEncoding=utf8&useSSL=truedriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: root#配置數(shù)據(jù)源的類(lèi)型type: org.apache.commons.dbcp2.BasicDataSourcetype: org.springframework.jdbc.datasource.DriverManagerDataSource

使用druid【自己寫(xiě)自動(dòng)配置類(lèi)】

修改pom.xml加入druid的依賴(lài)

<!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.21</version> </dependency>

創(chuàng)建配置類(lèi)MyDruidAutoConfiguration

package com.sxt.config;import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;@Configuration @ConditionalOnClass(value = {DruidDataSource.class}) @EnableConfigurationProperties(MyDruidProperties.class) //把MyDruidProperties類(lèi)注入到ioc容器中 public class MyDruidAutoConfiguration {private Log log= LogFactory.getLog(MyDruidAutoConfiguration.class);@Autowiredprivate MyDruidProperties properties;/*** 創(chuàng)建DataSource*/@Bean(initMethod = "init")public DruidDataSource druidDataSource(){DruidDataSource dataSource=new DruidDataSource();if(null==properties.getUrl()){log.error("url can not be null");throw new RuntimeException("url can not be null");}dataSource.setDriverClassName(properties.getDriverClassName());dataSource.setUrl(properties.getUrl());dataSource.setUsername(properties.getUsername());dataSource.setPassword(properties.getPassword());dataSource.setMaxActive(properties.getMaxActive());dataSource.setMinIdle(properties.getMinIdle());dataSource.setMaxIdle(properties.getMaxIdle());dataSource.setInitialSize(properties.getInitialSize());dataSource.setValidationQuery(properties.getValidationQuery());return dataSource;}/*** 注冊(cè)監(jiān)聽(tīng)頁(yè)面的Servlet*/@Bean@ConditionalOnClass(value = {StatViewServlet.class})public ServletRegistrationBean<StatViewServlet> registrationBeanStatViewServlet(){//創(chuàng)建注冊(cè)器ServletRegistrationBean<StatViewServlet> bean=new ServletRegistrationBean<>();//創(chuàng)建ServletStatViewServlet servlet=new StatViewServlet();//注冊(cè)bean.setServlet(servlet);//注入相關(guān)參數(shù)bean.addInitParameter("loginUsername",properties.getStatView().getLoginUsername());bean.addInitParameter("loginPassword",properties.getStatView().getLoginPassword());bean.addInitParameter("allow",properties.getStatView().getAllow());bean.addInitParameter("deny",properties.getStatView().getDeny());if(properties.getStatView().getUrlMapping()==null||properties.getStatView().getUrlMapping().length==0){log.error("監(jiān)控的urlMapping不能為空");throw new RuntimeException("監(jiān)控的urlMapping不能為空");}//設(shè)置映射bean.addUrlMappings(properties.getStatView().getUrlMapping());return bean;} }

創(chuàng)建屬性類(lèi)MyDruidProperties

package com.sxt.config;import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties;@Data @ConfigurationProperties(prefix = "spring.druid") public class MyDruidProperties {private String driverClassName;private String url;private String username;private String password;private Integer initialSize;private Integer maxActive;private Integer minIdle;private Integer maxIdle;private String validationQuery; //連接檢查語(yǔ)句private StatView statView;@Datastatic class StatView{//監(jiān)控的屬性private String loginUsername;private String loginPassword;private String allow;private String deny;private String [] urlMapping;} }

修改yml

spring:#數(shù)據(jù)源druid:url: jdbc:mysql://127.0.0.1:3306/dd?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTCdriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: rootmax-active: 20initial-size: 5min-idle: 5max-idle: 10validation-query: select 1stat-view:login-username: adminlogin-password: adminallow:deny:url-mapping:- "/druid/*"

排除DataSoruceAutoConfiguration

package com.sxt;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;//啟動(dòng)時(shí)排除DataSourceAutoConfiguration自動(dòng)配置類(lèi)的加載 @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}


數(shù)據(jù)源加載不出來(lái)


其它錯(cuò)

總結(jié)

以上是生活随笔為你收集整理的数据源配置和自动管理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。