赞
踩
引入依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.8</version> </dependency> <!-- jdbc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
spring:
datasource:
username: root
password: root
url: jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
@Value("spring.datasource.url") private String url; @Value("spring.datasource.username") private String userName; @Value("spring.datasource.password") private String password; @Value("spring.datasource.driver-class-name") private String driverClassName; @Value("spring.datasource.type") private String type; @Bean public DataSource dataSource(){ DruidDataSource dataSource = new DruidDataSource(); dataSource.setUrl(url); dataSource.setUsername(userName); dataSource.setPassword(password); dataSource.setDriverClassName(driverClassName); dataSource.setDbType(type); return dataSource; }
2.使用注解ConfigurationProperties
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource(){
return new DruidDataSource();
}
3.使用DataSourceProperties构造者设计模式
@Bean
public DataSource dataSource(DataSourceProperties properties){
return properties.initializeDataSourceBuilder().build();
}
/** * 监控台的servlet */ @Bean public ServletRegistrationBean statViewServlet() { ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(); servletRegistrationBean.setServlet(new StatViewServlet()); servletRegistrationBean.addUrlMappings("/druid/*") // 添加IP白名单 servletRegistrationBean.addInitParameter("allow", "127.0.0.1"); // 添加IP黑名单,当白名单和黑名单重复时,黑名单优先级更高 // servletRegistrationBean.addInitParameter("deny", "127.0.0.1"); // 添加控制台管理用户 servletRegistrationBean.addInitParameter("loginUsername", "admin"); servletRegistrationBean.addInitParameter("loginPassword", "123456"); // 是否能够重置数据 servletRegistrationBean.addInitParameter("resetEnable", "false"); return servletRegistrationBean; } /** * 配置服务过滤器 :监控哪些访问 * @return 返回过滤器配置对象 */ @Bean public FilterRegistrationBean statFilter() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); filterRegistrationBean.setFilter(new WebStatFilter()); // 添加过滤规则 filterRegistrationBean.addUrlPatterns("/*"); // 忽略过滤格式 filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*,"); return filterRegistrationBean; }
本地访问http://localhost:8080/druid
添加常用启动器依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
依赖添加完成后,不需要创建数据源,可直接使用,但是访问Druid的控制台,却无法访问。
大致查看源码:
//创建一个DruidDataSourceWrapper数据源
@ConditionalOnMissingBean
public DataSource dataSource() {
LOGGER.info("Init DruidDataSource");
return new DruidDataSourceWrapper();
}
@ConfigurationProperties("spring.datasource.druid") public class DruidDataSourceWrapper extends DruidDataSource implements InitializingBean { @Autowired private DataSourceProperties basicProperties; public DruidDataSourceWrapper() { } //重写InitializingBean方法,springboot启动时,会自动加载 public void afterPropertiesSet() throws Exception { if(super.getUsername() == null) { //从对象basicProperties中拿设置的用户名 super.setUsername(this.basicProperties.determineUsername()); } if(super.getPassword() == null) { //从对象basicProperties中拿设置的用户名 super.setPassword(this.basicProperties.determinePassword()); } if(super.getUrl() == null) { super.setUrl(this.basicProperties.determineUrl()); } if(super.getDriverClassName() == null) { super.setDriverClassName(this.basicProperties.getDriverClassName()); } } } //查看basicProperties @ConfigurationProperties(prefix = "spring.datasource") public class DataSourceProperties implements BeanClassLoaderAware, InitializingBean { private ClassLoader classLoader; private String name; private boolean generateUniqueName; private Class<? extends DataSource> type; private String driverClassName; .... }
由此看出,在yml文件数据源的配置:spring.datasource.url等等
我们在回头看上面的截图中:DruidDataSourceAutoConfigure类上@Import注解中引入DruidStatViewServletConfiguration.class,我们可以点开看看:
@ConditionalOnWebApplication @ConditionalOnProperty( name = {"spring.datasource.druid.stat-view-servlet.enabled"}, havingValue = "true" ) public class DruidStatViewServletConfiguration { private static final String DEFAULT_ALLOW_IP = "127.0.0.1"; public DruidStatViewServletConfiguration() { } @Bean public ServletRegistrationBean statViewServletRegistrationBean(DruidStatProperties properties) { //获取Druid监控台配置信息 StatViewServlet config = properties.getStatViewServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(); registrationBean.setServlet(new com.alibaba.druid.support.http.StatViewServlet()); registrationBean.addUrlMappings(new String[]{config.getUrlPattern() != null?config.getUrlPattern():"/druid/*"}); if(config.getAllow() != null) { registrationBean.addInitParameter("allow", config.getAllow()); } else { registrationBean.addInitParameter("allow", "127.0.0.1"); } if(config.getDeny() != null) { registrationBean.addInitParameter("deny", config.getDeny()); } if(config.getLoginUsername() != null) { registrationBean.addInitParameter("loginUsername", config.getLoginUsername()); } if(config.getLoginPassword() != null) { registrationBean.addInitParameter("loginPassword", config.getLoginPassword()); } if(config.getResetEnable() != null) { registrationBean.addInitParameter("resetEnable", config.getResetEnable()); } return registrationBean; } }
ConditionalOnProperty注解做了限制,由此可看出yml需要配置spring.datasource.druid.stat-view-servlet.enabled属性,且为true。其他的用户名、密码如何配置呢
配置信息都是从StatViewServlet类中获取,查看一下代码
@ConfigurationProperties("spring.datasource.druid")
public class DruidStatProperties {
private String[] aopPatterns;
private DruidStatProperties.StatViewServlet statViewServlet = new DruidStatProperties.StatViewServlet();
public static class StatViewServlet {
private boolean enabled;
private String urlPattern;
private String allow;
private String deny;
private String loginUsername;
private String loginPassword;
private String resetEnable;
}
}
由此看出:设置账号的spring.datasource.druid.StatViewServlet.loginUsername
yml的格式spring.datasource.druid.stat-view-servlet.login-username
其他配置同理
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。