这是我的配置文件
# 国际化配置文件(包名.基础名) spring.messages.basename=i18n.login server.tomcat.uri-encoding=UTF-8 spring.mvc.date-format=yyyy-MM-dd # 禁用缓存 spring.thymeleaf.cache=false #ctrl+f9 重新编译页面 spring.datasource.password=root spring.datasource.username=root spring.datasource.url=jdbc:mysql://localhost:3306/jdbc spring.datasource.driver-class-name=com.alibaba.druid.proxy.DruidDriver #黄色背景如果不进行注册那么就不会起作用 spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive= 20 spring.datasource.maxWait= 60000 spring.datasource.timeBetweenEvictionRunsMillis= 60000 spring.datasource.minEvictableIdleTimeMillis= 300000 spring.datasource.validationQuery= SELECT 1 FROM DUAL spring.datasource.testWhileIdle= true spring.datasource.testOnBorrow= false spring.datasource.testOnReturn= false spring.datasource.poolPreparedStatements= true # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙 spring.datasource.filters= stat,wall,log4j spring.datasource.maxPoolPreparedStatementPerConnectionSize= 20 spring.datasource.useGlobalDataSourceStat=true spring.datasource.connectionProperties= druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 #指定全局配置文件的位置 #mybatis.config-location=classpath:mybatis/mybatis-config.xml #指定sql映射文件的位置 #mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
红色部分,如果不进行注册就不会起作用 ,写一个配置文件指出来
package cn.edu.aynu.springboot.zhaoyujing.Config; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import javax.servlet.Filter; import javax.servlet.Servlet; import javax.sql.DataSource; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @Configuration public class DruidConfig { @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druid(){ return new DruidDataSource(); } //配置Druid的监控 //配置一个管理后台的Servlet @Bean public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean<Servlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); Map<String, String> initParams = new HashMap<>(); initParams.put("loginUsername","admin"); initParams.put("loginPassword","123456"); initParams.put("allow","");//默认就是允许所有访问 initParams.put("deny","192.168.15.21"); bean.setInitParameters(initParams); return bean; } /*配置一个web监管的filter*/ @Bean public FilterRegistrationBean webStatFilter(){ final FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>(); bean.setFilter(new WebStatFilter()); Map<String,String> initParams = new HashMap<>(); bean.setInitParameters(initParams); bean.setUrlPatterns(Arrays.asList("/*")); return bean; } }
但是却报错了
Description: Failed to bind properties under 'spring.datasource' to javax.sql.DataSource: Property: spring.datasource.filters Value: stat,wall,log4j Origin: class path resource [application.properties]:26:28 Reason: org.apache.log4j.Logger Action: Update your application's configuration Process finished with exit code 1
根据提示可知需要加上一个就可以解决了
<!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>