Spring PropertyPlaceholderConfigurer数据库配置

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: pom.xml中添加依赖 mysql mysql-connector-java 5.1.38 com.alibaba druid 1.0.20属性配置文件:system-config.

pom.xml中添加依赖

<!-- mysql-connector-java -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.38</version>
</dependency>

<!-- druid -->
<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>druid</artifactId>
  <version>1.0.20</version>
</dependency>
AI 代码解读

属性配置文件:system-config.properties

################################################
# DataSource Config
jdbc.url=jdbc\:mysql\://127.0.0.1:3306/quartz?useUnicode\=true&characterEncoding\=utf8&autoReconnect\=true&useSSL\=false&zeroDateTimeBehavior\=convertToNull
jdbc.username=root
jdbc.publicKey=MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALIT9sFn8U+Hoo80Q+Hepwc0ZN6HBiyAW4SiLCXLhNNjxB45mtRamABoB0O9dEsziT/gwtuXMuC2bWePdCvEb1ECAwEAAQ==
jdbc.password=fCHxOiDBDsWY/BJLg05fbNGvQmDRPZJufcvyqCqml+zwmB4Gw/Bn7lzy8w117CQ1jEBFpj0ERgQsCBJD0ROfJw==
AI 代码解读

applicationContext.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 只需要加载service、dao即可,不需要加载controller -->
    <context:component-scan base-package="com.quartz">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </context:component-scan>

    <!-- 占位符 -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:system-config.properties</value>
            </list>
        </property>
    </bean>

    <!-- 防SQL注入过滤器 -->
    <bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
        <property name="dbType" value="mysql" />
    </bean>
    <!-- 监控信息过滤器 -->
    <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
        <!-- slowSqlMillis用来配置SQL慢的标准,执行时间超过slowSqlMillis的就是慢。 -->
        <property name="slowSqlMillis" value="10000" />
        <property name="logSlowSql" value="true" />
        <property name="mergeSql" value="true" />
    </bean>

    <!-- 数据源 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- 基本属性 url、user、password -->
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <!-- 密码加密 -->
        <property name="filters" value="config" />
        <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${jdbc.publicKey}" />
        <property name="password" value="${jdbc.password}" />

        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="5" />
        <property name="minIdle" value="5" />
        <property name="maxActive" value="30" />

        <!-- 配置获取连接等待超时的时间 -->
        <property name="maxWait" value="60000" />

        <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />

        <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000" />

        <property name="validationQuery" value="SELECT 'x'" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />

        <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
        <property name="poolPreparedStatements" value="false" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />

        <!-- 超过时间限制是否回收 -->
        <property name="removeAbandoned" value="true" />
        <!-- 超时时间;单位为秒。180秒=3分钟 -->
        <property name="removeAbandonedTimeout" value="180" />
        <!-- 关闭abanded连接时输出错误日志 -->
        <property name="logAbandoned" value="true" />
        <property name="proxyFilters">
            <list>
                <!-- 监控信息过滤器 -->
                <ref bean="stat-filter" />
                <!-- 防注入的话从前台传排序字段排序不好用 -->
                <ref bean="wall-filter" />
            </list>
        </property>
    </bean>

</beans>
AI 代码解读
相关实践学习
如何快速连接云数据库RDS MySQL
本场景介绍如何通过阿里云数据管理服务DMS快速连接云数据库RDS MySQL,然后进行数据表的CRUD操作。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
打赏
0
0
0
0
2
分享
相关文章
Spring系列文章4:读取配置文件
Spring系列文章4:读取配置文件
103 0
【小家Spring】Spring中读取配置的方式,@Value、@PropertySource、@ConfigurationProperties使用详解(下)
【小家Spring】Spring中读取配置的方式,@Value、@PropertySource、@ConfigurationProperties使用详解(下)
【小家Spring】Spring中读取配置的方式,@Value、@PropertySource、@ConfigurationProperties使用详解(下)
Spring JDBC-使用XML配置声明式事务
Spring JDBC-使用XML配置声明式事务
165 0
【小家Spring】Spring中读取配置的方式,@Value、@PropertySource、@ConfigurationProperties使用详解(上)
【小家Spring】Spring中读取配置的方式,@Value、@PropertySource、@ConfigurationProperties使用详解(上)
【小家Spring】Spring中读取配置的方式,@Value、@PropertySource、@ConfigurationProperties使用详解(中)
【小家Spring】Spring中读取配置的方式,@Value、@PropertySource、@ConfigurationProperties使用详解(中)
spring+springmvc+hibernate整合实例
最近要弄一个自动化生成表及其实体对应的增删改查的框架,于是我想到了hibernate,hibernate就有根据实体自动建表,而且增删改查,都不需要想mybatis那样在xml文件中配置。 不过怎样让该框架通过前端一个表单页面配置,从而让后台对应的生成,这个我还没想明白,不过说到这,不得不提传统的ssh框架,传统的ssh框架,通常是指spring+struts2+hibernate或spring+struts+hibernate 不过想到struts2就有恐惧感,太多的action和xml配置,比较繁琐,不如springmvc来的痛快。
1352 0
spring4整合hibernate5.1
<div class="markdown_views"> <p>在另一篇文章<a href="http://blog.csdn.net/qwe6112071/article/details/51010652">hibernate5(2)初入门配置实例</a>中,我们针对hibernate5.1版本的崭新引导配置方法,完成了对数据库的的插入实例操作,在本节内容中,我们开始引入sp
3208 0
浅谈Spring的PropertyPlaceholderConfigurer
转自:http://blog.csdn.net/blueboz/article/details/54808915 转自:https://www.cnblogs.
674 0
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等