| | |
| | | package com.ltkj.framework.config; |
| | | |
| | | import java.io.FileInputStream; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.Properties; |
| | | import javax.annotation.Resource; |
| | | import javax.servlet.Filter; |
| | | import javax.servlet.FilterChain; |
| | | import javax.servlet.ServletException; |
| | |
| | | |
| | | import com.ltkj.framework.config.properties.DruidProperties; |
| | | import com.ltkj.framework.datasource.DynamicDataSource; |
| | | import com.ltkj.system.service.ISysConfigService; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | import org.springframework.boot.web.servlet.FilterRegistrationBean; |
| | |
| | | */ |
| | | @Configuration |
| | | public class DruidConfig { |
| | | |
| | | @Bean |
| | | @ConfigurationProperties("spring.datasource.druid.master") |
| | | // @ConfigurationProperties("spring.datasource.druid.master") |
| | | public DataSource masterDataSource(DruidProperties druidProperties) { |
| | | DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); |
| | | Properties props = new Properties(); |
| | | try { |
| | | // 从文件中读取配置信息 |
| | | FileInputStream fis = new FileInputStream("D:\\ltkjprojectconf\\config.properties"); |
| | | props.load(fis); |
| | | fis.close(); |
| | | // 获取属性值并赋值 |
| | | Properties properties = new Properties(); |
| | | // 这里是测试写法,具体的value可以通过请求参数传递过来 |
| | | properties.setProperty("druid.url",props.getProperty("url")); |
| | | properties.setProperty("druid.username",props.getProperty("username")); |
| | | properties.setProperty("druid.password",props.getProperty("password")); |
| | | dataSource.restart(properties); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return druidProperties.dataSource(dataSource); |
| | | } |
| | | |