栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【SpringBoot】SpringBoot框架中如何向web.xml中添加配置(以配置DataSource的后台监控功能为例)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【SpringBoot】SpringBoot框架中如何向web.xml中添加配置(以配置DataSource的后台监控功能为例)

SpringBoot框架中已经没有web.xml文件了,那么如何向web.xml中添加配置呢?比如如何配置filter等?

1.通过application.yaml文件设置配置详情。需要引入log4j的maven依赖。

spring:
  datasource:
    username: root
    password: 
    url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource

    #druid 数据源专有配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECt 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true

    #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500

2.新建MyConfig类,并标注@Configuration注解。

3.通过@Bean、@ConfigurationProperties注解绑定配置文件。

@Configuration
public class MyDataSourceConfig {
	@ConfigurationProperties(prefix="spring.datasource")
	@Bean
	public DataSource getDataSource(){
		return new DruidDataSource();
	}
	//添加后台监控功能
	@Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
        Map initParams = new HashMap<>();
        initParams.put("loginUsername", "root");
        initParams.put("loginPassword", "12581.gzh");
        bean.setInitParameters(initParams);
        return bean;
    }
}

SpringBoot框架中内置了Servlet,因此没有web.xml文件了。想要实现与MVC相关的功能,就通过ServletRegistrationBean这个泛型类。

使用示例:ServletRegistrationBean bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/643110.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号