关于springboot集成druid使用Environment读取配置文件并创建Bean的问题,我创建了一个动态数据源,但是它抛错了.错误日志我贴出来了,有人能帮我看下吗?我必须要要使用Environment读取配置文件,因为只有在项目启动的时候,我才知道数据源配了有几个,它是动态的。
@Bean("localDataSource")
public DataSource getDataSource(){
DataSourceProperties dataSourceProperties = new DataSourceProperties(Binder.get(environment).bind("spring.datasource",Map.class).orElseThrow(() -> new IllegalStateException("Unable to bind to spring.datasource property")));
Map<Object, Object> dataSourceMap = new HashMap<>();
dataSourceProperties.getRouting().forEach((k,v)->{
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(v.get("driver-class-name").toString());
dataSource.setUrl(v.get("url").toString());
dataSource.setPassword(v.get("password").toString());
dataSource.setUsername(String.valueOf(v.get("username").toString()));
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setMapUnderscoreToCamelCase(true); // 开启驼峰命名规则
// 注册 mybatis自定义type处理
configuration.getTypeHandlerRegistry().register(localObjectTypeHandler);
dataSourceMap.put(k, dataSource);
});
DataSourceRouting dataSourceRouting = new DataSourceRouting();
dataSourceRouting.setTargetDataSources(dataSourceMap);
dataSourceRouting.setDefaultTargetDataSource(dataSourceProperties.getRouting().keySet().stream().findAny().get());
return dataSourceRouting;
}
@Mapper
@DependsOn("localDataSource")
public interface TaskMapper{
@Select("select * from (${sql}) b")
List<Map<String,String>> select(String sql);
@Select("select * from (${sql}) b limit #{startIndex} ,#{pageSize}")
List<Map<String,String>> select(@Param("sql") String sql, @Param("startIndex") long startIndex,@Param("pageSize") int pageSize);
@Select("select count(0) from (${sql}) b")
int count(String sql);
}
error log
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobProcess': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mysqlServiceImpl': Unsatisfied dependency expressed through field 'taskMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'localDataSource' defined in class path resource [com/weimj/config/DataSourceConfiguration.class]: Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.lookup.DataSourceLookupFailureException: Failed to look up JNDI DataSource with name 'local_test'; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or in an application resource file: java.naming.factory.initial.
http://localhost:8001/druid/login.html
,如下:admin/admin
针对您的问题,您需要按照以下步骤配置多个数据源并在不同的代码段中使用它们:
1.添加依赖:按照参考资料中的要求添加Druid的依赖。
2.配置多个数据源: 首先在配置文件中配置数据源的属性,例如:
spring.datasource.druid.first.url=jdbc:mysql://localhost:3306/test spring.datasource.druid.first.username=root spring.datasource.druid.first.password=root spring.datasource.druid.first.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.second.url=jdbc:mysql://localhost:3306/test2 spring.datasource.druid.second.username=root spring.datasource.druid.second.password=root spring.datasource.druid.second.driver-class-name=com.mysql.jdbc.Driver
然后在代码中定义数据源的Bean,并使用@ConfigurationProperties将配置文件中的属性绑定到Bean中,例如:
@Configuration public class DataSourceConfig {
@Bean(name = "firstDataSource")
@Qualifier("firstDataSource")
@ConfigurationProperties(prefix = "spring.datasource.druid.first")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().type(DruidDataSource.class).build();
}
@Bean(name = "secondDataSource")
@Qualifier("secondDataSource")
@ConfigurationProperties(prefix = "spring.datasource.druid.second")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().type(DruidDataSource.class).build();
}
}
3.在不同的代码段中使用不同的数据源:在需要使用不同数据源的地方,使用@Qualifier注解指定数据源的名称,例如:
@Autowired @Qualifier("firstDataSource") DataSource firstDataSource;
@Autowired @Qualifier("secondDataSource") DataSource secondDataSource;
以上就是将Druid集成到Spring Boot中,配置多个数据源并在不同的代码段中使用的具体步骤,希望能对您有所帮助。