package com.boot.config; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.sql.DataSource; /** * @Author 36140 * @Date 2022/2/19 17:20 * @Version 1.0 */ @Configuration public class DataSourceConfig { /** * 默认的自动配置是判断容器中没有数据库连接池才会配置 * 这里使用ConfigurationProperties 直接复用"spring.datasource" 的配置文件 * * 关于druid 1.1.0 之后。打开 druid页面监控不显示情况。 可以注入bean 的时候,添加初始化,和销毁方法 * * @return */ @ConfigurationProperties("spring.datasource") @Bean(destroyMethod = "close", initMethod = "init") public DataSource dataSource(){ DruidDataSource source = new DruidDataSource(); return source; } /** * 配置druid 监控 * 注入原生的 servlet组件, ServletRegistrationBean */ @Bean public ServletRegistrationBean statViewServlet(){ StatViewServlet viewServlet = new StatViewServlet(); ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(viewServlet,"/druid/*"); return registrationBean; } }