package com.Config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

/**
 * spring 核心配置文件类
 */
@Configuration
// 组件扫描
//<context:component-scan base-package="com"></context:component-scan>
@ComponentScan({"com"})
@PropertySource("classpath:jdbc.properties")
public class SpringConfiguration {
    // 写一个dataSource
    @Value("${jdbc.url}")
    String jdbcUrl;
    @Value("${jdbc.driver}")
    String driver;
    @Value("${jdbc.username}")
    String userName;
    @Value("${jdbc.password}")
    String password;
    @Bean("getJdbcUrl")
    public String getJdbcUrl() {
        return jdbcUrl;
    }
    @Bean("dataSource")
    public DataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(driver);
        druidDataSource.setUrl(jdbcUrl);
        druidDataSource.setUsername(userName);
        druidDataSource.setPassword(password);
        return druidDataSource;
    }


}
