数据源配置类

package com.ustcinfo.hibernate.dbSourseConfig;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

import com.alibaba.druid.pool.DruidDataSource;

/**
* 数据源配置类
*/
@Configuration
@PropertySource("classpath:datasource.properties") // 选择配置文件的来源
public class DruidDBConfig {

private static Logger LOG_DB = LoggerFactory.getLogger(DruidDBConfig.class);

@Value("${spring.datasource.url}")
private String dbUrl;

@Value("${spring.datasource.username}")
private String username;

@Value("${spring.datasource.password}")
private String password;

@Value("${spring.datasource.driverClassName}")
private String driverClassName;

@Value("${spring.datasource.initialSize}")
private int initialSize;

@Value("${spring.datasource.minIdle}")
private int minIdle;

@Value("${spring.datasource.maxActive}")
private int maxActive;

@Value("${spring.datasource.maxWait}")
private int maxWait;

@Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
private int timeBetweenEvictionRunsMillis;

@Value("${spring.datasource.minEvictableIdleTimeMillis}")
private int minEvictableIdleTimeMillis;

@Value("${spring.datasource.validationQuery}")
private String validationQuery;

@Value("${spring.datasource.testWhileIdle}")
private boolean testWhileIdle;

@Value("${spring.datasource.testOnBorrow}")
private boolean testOnBorrow;

@Value("${spring.datasource.testOnReturn}")
private boolean testOnReturn;

@Value("${spring.datasource.poolPreparedStatements}")
private boolean poolPreparedStatements;

@Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
private int maxPoolPreparedStatementPerConnectionSize;

@Value("${spring.datasource.filters}")
private String filters;

@Value("{spring.datasource.connectionProperties}")
private String connectionProperties;

@Bean(name = "dataSource") // 声明其为Bean实例
@Primary // 在同样的DataSource中,首先使用被标注的DataSource
public DataSource dataSource() {

DruidDataSource datasource = new DruidDataSource();

datasource.setUrl(dbUrl);
datasource.setUsername(username);
datasource.setPassword(password);
datasource.setDriverClassName(driverClassName);

// 配置信息
datasource.setInitialSize(initialSize);
datasource.setMinIdle(minIdle);
datasource.setMaxActive(maxActive);
datasource.setMaxWait(maxWait);
datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
datasource.setValidationQuery(validationQuery);
datasource.setTestWhileIdle(testWhileIdle);
datasource.setTestOnBorrow(testOnBorrow);
datasource.setTestOnReturn(testOnReturn);
datasource.setPoolPreparedStatements(poolPreparedStatements);
datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
try {
datasource.setFilters(filters);
} catch (SQLException e) {
LOG_DB.error("druid configuration initialization filter", e);
}
datasource.setConnectionProperties(connectionProperties);

return datasource;
}
}

时间: 2024-07-31 13:27:07

数据源配置类的相关文章

Spring Boot学习进阶笔记(四)-多数据源配置(JdbcTemplate、Spring-data-jpa)

在实际开发过程中,往往我们需要链接多个数据库进行操作,所以多数据源的配置就在所难免了. 一.JdbcTemplate支持: spring boot配置多数据源比较简单 1)修改配置文件"application.properties" p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Monaco; color: #3933ff } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0

spring boot 多数据源配置(多种数据库)

最近一段时间在使用spring boot开发项目,其中有一个项目用到了多数据源的配置,网上的资料还是不太多,走了好多才找到一个合适的,把自己写的分享一下,做个笔记,以后也许有用,第一次写博客,不好勿喷!! 首先介绍下我的业务场景,此项目用到了两种数据库,一个是mysql,另一个是sqlserver, 首先第一步需要在application.yml中将多数据源的配置信息进行配置, mysql数据源: spring: datasource: driverClassName: com.mysql.jd

spring 4 + jpa(hibernate 3/4) + spring mvc 多数据源配置

先从persistence.xml开始: <?xml version=”1.0″ encoding=”UTF-8″?><persistence version=”2.1″ xmlns=”http://java.sun.com/xml/ns/persistence” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence

Spring Boot之JdbcTemplate多数据源配置与使用

之前在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源.在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.properties文件中配置连接参数即可.但是往往随着业务量发展,我们通常会进行数据库拆分或是引入其他数据库,从而我们需要配置多个数据源,下面基于之前的JdbcTemplate和Spring-data-jpa例子分别介绍两种多数据源的配置方式. 多数据源配置 创建一个Spring配置类,定义两个DataSource

数据源配置

转自:spring入门(六)[springMVC中各数据源配置]  && Spring下配置几种常用连接池 在使用spring进行javaWeb开发的过程中,需要和数据库进行数据交换,为此要经常获取数据库连接,使用JDBC的方式获取数据库连接,使用完毕之后再释放连接,这种过程对系统资源的消耗无疑是很大的,这里简单描述三种数据库连接池的配置,使用这些连接池可以获得一个数据源.1.spring自带的JDBC连接池:2.c3p0:3.dbcp: 一.spring自带的JDBC方式 spring提

spring(16)------spring的数据源配置

在spring中,通过XML的形式实现数据源的注入有三种形式. 一,使用spring自带的DriverManagerDataSource 使用DriverManagerDataSource配置数据源与直接使用jdbc在效率上没有多大的区别,使用DriverManagerDataSource配置数据源 的代码实例如下,这里重点研究spring的数据源配置,就采用spring编程式事务处理来来研究数据源的配置. 所需要的jar包和spring编程式配置:http://blog.csdn.net/yh

jpa多数据源配置参考链接

jpa单数据源配置: http://doc.okbase.net/liuyitian/archive/109276.html jpa多数据源配置: http://www.blogjava.net/weir/archive/2015/01/08/422132.html jpa不在persistence.xml文件中配置每个Entity实体类的2中解决办法 http://www.cnblogs.com/taven/archive/2013/10/04/3351841.html spring+jpa两

项目重构之数据源配置与优化:log4j 配置数据库连接池Druid,并实现日志存储到数据库

作者:泥沙砖瓦浆木匠 个人签名:打算起手不凡写出鸿篇巨作的人,往往坚持不了完成第一章节. 如果我的帮到了你,是否乐意捐助一下或请一杯啤酒也好呢?有你支持,干的更好~ 点这参与众筹 我的支付宝:13958686678 一. 前言 泥瓦匠又和大家见面了,最近两天我在Code Review ,顺便代码小小的Refactoring(重构)下.先了解这个项目吧,这次解决的是数据源配置优化.因为这web项目中配置数据源的地方很多.例如JDBC要配置数据源,Mybatis要配置数据源,Quartz定时任务要配

Spring BeanPostProcessor与动态加载数据源配置

前言: 本文旨在介绍Spring动态配置数据源的方式,即对一个DataSource的配置诸如jdbcUrl,user,password,driverClass都通过运行时指定,而非由xml静态配置. Spring构造Context的参数一般只包含配置文件路径和类加载器,如果需要达到动态传入配置参数的目的,需要Spring在初始化数据源相关bean的时候能够对原有配置执行修改或替换,为方便处理,本文将定义一个名为DynamicDataSourceConfigHolder的公共类提供配置数据存储.