2018-08-23

2018-08-24  本文已影响0人  城铠唐

<code>

package com.zhidisoft.config;

import java.beans.PropertyVetoException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.PropertySource;

import org.springframework.core.env.Environment;

import com.mchange.v2.c3p0.ComboPooledDataSource;

//当前类是配置类 ,可以当做是配置文件

@Configuration   

//将properties文件引入到配置类中

@PropertySource(value="classpath:jdbc.properties") 

public class SpringConfig {

    @Bean

    public DataSource dataSource(@Autowired Environment env) throws PropertyVetoException{

        ComboPooledDataSource source = new ComboPooledDataSource();

        source.setDriverClass(env.getProperty("jdbc.mysql.driver"));

        source.setJdbcUrl(env.getProperty("jdbc.mysql.url"));

        source.setUser(env.getProperty("jdbc.mysql.user"));

        source.setPassword(env.getProperty("jdbc.mysql.password"));

        source.setMaxPoolSize(10);

        source.setMinPoolSize(5);

        source.setInitialPoolSize(6);

        source.setMaxConnectionAge(28800);

        source.setMaxIdleTime(21600);

        return source;

    }

}

</code>

上一篇下一篇

猜你喜欢

热点阅读