springboot配置文件部分未生效 springboot 配置文件里部分配置未生效的解决

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

springboot配置文件部分未生效 springboot 配置文件里部分配置未生效的解决

poosang   2021-08-12 我要评论
想了解springboot 配置文件里部分配置未生效的解决的相关内容吗,poosang在本文为您仔细讲解springboot配置文件部分未生效的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:springboot配置,配置文件,配置未生效,下面大家一起来学习吧。

springboot 配置文件里部分配置未生效

最近用springboot搭了个项目,上线过段时间就会出现卡死,猜测是数据库连接池的连接被占满,用的连接池是druid,于是给项目加上了一个数据库连接池监控。

代码如下:

@Configuration
public class DruidConfiguration {
    
    /**
     * 
     * 注册一个StatViewServlet
     * 
     * @return
     * 
     */
 
    @Bean
    public ServletRegistrationBean DruidStatViewServle2() { 
        // org.springframework.boot.context.embedded.ServletRegistrationBean提供类的进行注册. 
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),
                "/druid/*");
 
        // 添加初始化参数:initParams 
        // 白名单: 
//        servletRegistrationBean.addInitParameter("allow", "127.0.0.1"); 
        // IP黑名单 (存在共同时,deny优先于allow) : 如果满足deny的话提示:Sorry, you are not
        // permitted to view this page. 
//        servletRegistrationBean.addInitParameter("deny", "192.168.1.73"); 
        // 登录查看信息的账号密码. 
        servletRegistrationBean.addInitParameter("loginUsername", "admin"); 
        servletRegistrationBean.addInitParameter("loginPassword", "admin"); 
        // 是否能够重置数据. 
        servletRegistrationBean.addInitParameter("resetEnable", "false"); 
        return servletRegistrationBean; 
    }
 
    /**
     * 
     * 注册一个:filterRegistrationBean
     * 
     * @return
     * 
     */
 
    @Bean
    public FilterRegistrationBean druidStatFilter2() { 
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); 
        // 添加过滤规则.
 
        filterRegistrationBean.addUrlPatterns("/*"); 
        // 添加不需要忽略的格式信息.
 
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid2/*"); 
        return filterRegistrationBean; 
	}
}

于是重启项目,进入监控页面发现与配置文件里面的部分配置对应不上,当时也没在意,以为是显示的默认配置。过阵子又卡死了,发现等待获取连接的线程数有10来个,果然和前面预料到的一样。于是在配置文件里面各种改数据库连接池的配置。

但,并没有什么卵用,因为项目根本就没有读取到这些配置,这个问题,网上也没能找到类似的文章和解决方案,到现在也没有发现问题出现在哪儿,最后的解决办法是将配置文件里面关于数据库的配置全都注释掉,加上了一个java类来配置

代码如下:

/**
 * druid数据连接池配置
 * @author win 10
 *
 */
@Configuration
public class DatasourceConfig {
 
 @Bean
 public DruidDataSource druidDataSource() {
        //Druid 数据源配置
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://127.0.0.1/autoorder?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        //初始连接数(默认值0)
        dataSource.setInitialSize(3);
        //最小连接数(默认值0)
        dataSource.setMinIdle(1);
        //最大连接数(默认值8,注意"maxIdle"这个属性已经弃用)
        dataSource.setMaxActive(20);
        
        dataSource.setMaxWait(30000);
        try {
   dataSource.setFilters("stat,wall,slf4j");
  } catch (SQLException e) {
   e.printStackTrace();
  }        
        dataSource.setTestWhileIdle(true);
        dataSource.setValidationQuery("SELECT 1");
        dataSource.setTimeBetweenEvictionRunsMillis(60000);
        dataSource.setMinEvictableIdleTimeMillis(30000);
        dataSource.setTestOnBorrow(true);
        dataSource.setTestOnReturn(false);        
        return dataSource;
    }
}

重启项目进入发现配置is working!卡死的问题解决,但是还是未能找到为什么通过resource里面的配置文件部分配置不生效的原因。

贴出配置文件:

# 服务启动端口
server.port=8776
#定时器开关
server.scheduler.syncorder=false
server.scheduler.xepnr=false
 
# 运维管理相关参数
timeout.host=5000
timeout.project=5000
 
#spring.http.encoding.force=true
#spring.http.encoding.charset=UTF-8
#spring.http.encoding.enabled=true
#server.tomcat.uri-encoding=UTF-8
 
spring.thymeleaf.content-type=text/html 
spring.thymeleaf.cache=false
spring.thymeleaf.mode=LEGACYHTML5
 
# jdbc_config   datasource
#spring.datasource.url=jdbc:mysql://127.0.0.1:3306/autoorder?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
#spring.datasource.username=root
#spring.datasource.password=root
#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#spring.datasource.maxActive=20
#spring.datasource.initialSize=1
#spring.datasource.minIdle=3
#spring.datasource.maxWait=20000
#连接空闲时长,超过时则会检查是否可用,与test-while-idle搭配
#spring.datasource.timeBetweenEvictionRunsMillis=60000
#spring.datasource.minEvictableIdleTimeMillis=300000
#连接空闲时检查是否可用
#spring.datasource.testWhileIdle=true
#每次获取连接时 检查是否可用
#spring.datasource.testOnBorrow=true
#每次归还连接时 检查是否可用
#spring.datasource.testOnReturn=fasle
#缓存游标是否开启
#spring.datasource.poolPreparedStatements=false
#spring.datasource.maxPoolPreparedStatementPerConnectionSize=20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
#spring.datasource.filters=stat,wall,slf4j
#验证数据库连接的有效性的sql
#spring.datasource.validationQuery=SELECT 1
#开启连接回收机制
#spring.datasource.removeAbandoned=true
#单位 s
#spring.datasource.removeAbandonedTimeout=180
#spring.datasource.timeBetweenEvictionRunsMillis=300000
  
# mybatis_config
mybatis.mapper-locations= classpath:org/jc/db/mapper/*Mapper.xml 
mybatis.typeAliasesPackage= org.jc.db.entity
#主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
global-config.id-type=0
##字段策略 0:"忽略判断",1:"非 NULL 判断"),2:"非空判断"
field-strategy= 2
#驼峰下划线转换
db-column-underline= true
#刷新mapper 调试神器
global-config.refresh-mapper= true
#数据库大写下划线转换
#capital-mode: true
#序列接口实现类配置
#key-generator: com.baomidou.springboot.xxx
#逻辑删除配置
#logic-delete-value: 0
#logic-not-delete-value: 1
#自定义填充策略接口实现
#meta-object-handler: com.baomidou.springboot.xxx
#自定义SQL注入器
#sql-injector: com.baomidou.springboot.xxx
 
## log_config   DEBUG    ERROR    INFO    WARN
#logging.level.root=info
##logging.level.io.z77z.dao= DEBUG
#logging.file= ./logs/express_interf.log
#logging.pattern.console= %d{yyyy/MM/dd-HH:mm:ss} [%thread] %-5level %logger- %msg%n
#logging.pattern.file= %d{yyyy/MM/dd-HH:mm} [%thread] %-5level %logger- %msg%n
 
spring.http.multipart.maxFileSize=100Mb
spring.http.multipart.maxRequestSize=200Mb

有看到的小伙伴知道这个问题所在的欢迎指点一二。

记录一次创建springboot 配置文件不生效的坑

使用idea自动生成了一个springboot项目。把application.properties改成了application.yml文件。打包成jar包运行。神奇的事情发生了,设置的端口不生效。

解决:

1.自己把yml文件改回properties文件。运行,仍旧不生效

2.上网百度。各种方案。然后还是不行。

3.突发奇想,因为我创建的项目是只需要一个五分钟循环执行的任务,所以我没导入web的maven。故导入。

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-websocket</artifactId>
  </dependency>

问题解决!!!!以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们