数据源配置和自动管理

使用DriverManagerDataSource

修改yml

#端口
server:
  port: 8080
spring:
  #数据源
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/dd?useUnicode=true&characterEncoding=utf8&useSSL=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    #配置数据源的类型
    type: org.springframework.jdbc.datasource.DriverManagerDataSource


使用DBCP

修改pom.xml引入dbcp的包

<!--加入Dbcp的依赖-->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-dbcp2</artifactId>
</dependency>
#端口
server:
  port: 8080
spring:
  #数据源
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/dym11?useUnicode=true&characterEncoding=utf8&useSSL=true
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    #配置数据源的类型
    type: org.apache.commons.dbcp2.BasicDataSource
    type: org.springframework.jdbc.datasource.DriverManagerDataSource


使用druid【自己写自动配置类】

修改pom.xml加入druid的依赖

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

创建配置类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类注入到ioc容器中
public class MyDruidAutoConfiguration {

    private Log log= LogFactory.getLog(MyDruidAutoConfiguration.class);

    @Autowired
    private MyDruidProperties properties;

    /**
     * 创建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;
    }

    /**
     * 注册监听页面的Servlet
     */
    @Bean
    @ConditionalOnClass(value = {StatViewServlet.class})
    public ServletRegistrationBean<StatViewServlet> registrationBeanStatViewServlet(){
        //创建注册器
        ServletRegistrationBean<StatViewServlet> bean=new ServletRegistrationBean<>();
        //创建Servlet
        StatViewServlet servlet=new StatViewServlet();
        //注册
        bean.setServlet(servlet);

        //注入相关参数
        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("监控的urlMapping不能为空");
            throw  new RuntimeException("监控的urlMapping不能为空");
        }
        //设置映射
        bean.addUrlMappings(properties.getStatView().getUrlMapping());

        return bean;

    }
}

创建属性类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;  //连接检查语句
    private StatView statView;

    @Data
    static class StatView{
       //监控的属性
       private String loginUsername;

       private String loginPassword;

       private String allow;

       private String deny;

       private String [] urlMapping;
    }
}

修改yml

spring:
  #数据源
  druid:
    url: jdbc:mysql://127.0.0.1:3306/dd?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: root
    max-active: 20
    initial-size: 5
    min-idle: 5
    max-idle: 10
    validation-query: select 1
    stat-view:
      login-username: admin
      login-password: admin
      allow:
      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;

//启动时排除DataSourceAutoConfiguration自动配置类的加载
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}



数据源加载不出来


其它错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值