我正在创建一个动态数据库spring启动应用程序。谁可以在运行时使用多个数据库。
问题是我在访问表数据时无法使用服务或存储库。
以下是我的配置类。
@Configuration
@EnableAutoConfiguration(exclude = {
DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class
})
@PropertySource({ "classpath:application.properties" })
@EnableTransactionManagement
@ComponentScan(basePackages = {"com.demo"})
public class AppConfig {
@Autowired
private Environment env;
@Resource
ConfigurableApplicationContext appContext;
@Bean
XmlConverter xmlConverter(){
XmlConverter xmlConverter = new XmlConverter();
xmlConverter.setMarshaller(castorMarshaller());
xmlConverter.setUnmarshaller(castorMarshaller());
return xmlConverter;
}
@Bean
public CastorMarshaller castorMarshaller(){
CastorMarshaller castorMarshaller = new CastorMarshaller();
castorMarshaller.setMappingLocation(appContext.getResource("classpath:mapping.xml"));
return castorMarshaller;
}
@Bean
public DataSource dataSource() {
AbstractRoutingDataSource dataSource = new DBRoutingDataSource();
Map<Object,Object> targetDataSources = new HashMap<>();
targetDataSources.put(DBContextHolder.DBName.COLL_AUSTRIA,lending_dataSource_9());
targetDataSources.put(DBContextHolder.DBName.COLL_BELGIUM,lending_dataSource_2());
targetDataSources.put(DBContextHolder.DBName.COLL_BRAZIL,lending_dataSource_11());
targetDataSources.put(DBContextHolder.DBName.COLL_CHILE,lending_dataSource_12());
targetDataSources.put(DBContextHolder.DBName.COLL_COLOMBIA,lending_dataSource_13());
targetDataSources.put(DBContextHolder.DBName.COLL_FRANCE,lending_dataSource_7());
targetDataSources.put(DBContextHolder.DBName.COLL_GERMANY,lending_dataSource_8());
targetDataSources.put(DBContextHolder.DBName.COLL_ITALY,lending_dataSource_1());
targetDataSources.put(DBContextHolder.DBName.COLL_MEXICO,lending_dataSource_14());
targetDataSources.put(DBContextHolder.DBName.COLL_NETHERLAND,lending_dataSource_3());
targetDataSources.put(DBContextHolder.DBName.COLL_SWISS,lending_dataSource_10());
targetDataSources.put(DBContextHolder.DBName.COLL_UK,lending_dataSource_4());
targetDataSources.put(DBContextHolder.DBName.CSS_BRAZIL,css_dataSource_11());
targetDataSources.put(DBContextHolder.DBName.CSS_CHILE,css_dataSource_12());
targetDataSources.put(DBContextHolder.DBName.CSS_COLOMBIA,css_dataSource_13());
targetDataSources.put(DBContextHolder.DBName.CSS_FRANCE,css_dataSource_7());
targetDataSources.put(DBContextHolder.DBName.CSS_GERMANY,css_dataSource_8());
targetDataSources.put(DBContextHolder.DBName.CSS_ITALY,css_dataSource_1());
targetDataSources.put(DBContextHolder.DBName.CSS_MEXICO,css_dataSource_14());
targetDataSources.put(DBContextHolder.DBName.CSS_UK,css_dataSource_4());
dataSource.setTargetDataSources(targetDataSources);
//dataSource.setDefaultTargetDataSource(lending_dataSource_11());
dataSource.afterPropertiesSet();
return dataSource;
}
public DataSource lending_dataSource_1() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_ITALY.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_ITALY.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_ITALY.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_ITALY.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource lending_dataSource_2() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_BELGIUM.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_BELGIUM.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_BELGIUM.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_BELGIUM.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource lending_dataSource_3() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_NETHERLAND.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_NETHERLAND.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_NETHERLAND.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_NETHERLAND.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource lending_dataSource_4() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_UK.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_UK.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_UK.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_UK.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource lending_dataSource_7() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_FRANCE.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_FRANCE.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_FRANCE.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_FRANCE.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource lending_dataSource_8() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_GERMANY.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_GERMANY.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_GERMANY.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_GERMANY.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource lending_dataSource_9() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_AUSTRIA.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_AUSTRIA.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_AUSTRIA.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_AUSTRIA.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource lending_dataSource_10() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_SWISS.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_SWISS.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_SWISS.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_SWISS.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource lending_dataSource_11() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_BRAZIL.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_BRAZIL.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_BRAZIL.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_BRAZIL.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource lending_dataSource_12() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_CHILE.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_CHILE.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_CHILE.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_CHILE.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource lending_dataSource_13() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_COLOMBIA.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_COLOMBIA.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_COLOMBIA.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_COLOMBIA.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource lending_dataSource_14() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_MEXICO.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_MEXICO.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_MEXICO.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_MEXICO.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource css_dataSource_1() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_BRAZIL.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_BRAZIL.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_BRAZIL.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_BRAZIL.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource css_dataSource_4() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_BRAZIL.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_BRAZIL.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_BRAZIL.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_BRAZIL.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource css_dataSource_8() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_BRAZIL.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_BRAZIL.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_BRAZIL.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_BRAZIL.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource css_dataSource_7() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_BRAZIL.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_BRAZIL.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_BRAZIL.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_BRAZIL.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource css_dataSource_11() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_BRAZIL.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_BRAZIL.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_BRAZIL.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_BRAZIL.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource css_dataSource_12() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_BRAZIL.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_BRAZIL.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_BRAZIL.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_BRAZIL.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource css_dataSource_13() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_BRAZIL.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_BRAZIL.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_BRAZIL.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_BRAZIL.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
public DataSource css_dataSource_14() {
DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
dataSourceBuilder.url(env.getProperty("COLL_BRAZIL.datasource.url"));
dataSourceBuilder.username(env.getProperty("COLL_BRAZIL.datasource.username"));
dataSourceBuilder.password(env.getProperty("COLL_BRAZIL.datasource.password"));
dataSourceBuilder.driverClassName(env.getProperty("COLL_BRAZIL.datasource.driver-class-name"));
return dataSourceBuilder.build();
}
@Autowired
@Bean(name = "transactionManager")
public DataSourceTransactionManager getTransactionManager(DataSource dataSource) {
DataSourceTransactionManager txManager = new DataSourceTransactionManager();
txManager.setDataSource(dataSource);
return txManager;
}
}
下面是DBContextHolder类:
public class DBContextHolder {
public enum DBName {
CSS_ITALY,
CSS_UK,
CSS_COLOMBIA,
CSS_BRAZIL,
CSS_GERMANY,
CSS_FRANCE,
CSS_CHILE,
CSS_MEXICO,
COLL_ITALY,
COLL_UK,
COLL_BELGIUM,
COLL_AUSTRIA,
COLL_NETHERLAND,
COLL_SWISS,
COLL_GERMANY,
COLL_FRANCE,
COLL_COLOMBIA,
COLL_BRAZIL,
COLL_CHILE,
COLL_MEXICO
}
private static final ThreadLocal<DBName> contextHolder = new ThreadLocal<DBName>();
private static DBName type = null;
public static void setDBName(DBName dbName) {
Assert.notNull(dbName, "customerType cannot be null");
contextHolder.set(dbName);
type = dbName;
contextHolder.set(type);
}
public static DBName getDBName() {
//return type;
return (DBName) contextHolder.get();
}
public static void clearDBName() {
type = null;
contextHolder.remove();
}
}
下面是DBRoutingDataSource类:
public class DBRoutingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DBContextHolder.getDBName();
}
}
以下是服务类:
public interface DBService {
public String getDBData(String sql);
}
以下是服务类:
@Service("DBService")
public class DBServiceImpl implements DBService{
@Autowired
DBDao DBDao;
@Override
public String getDBData(String sql) {
return DBDao.getDBData(sql);
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
下面是Repository Class:
public interface DBDao {
public String getDBData(String sql);
}
下面是Repository Class:
@Component
@Repository
@Transactional
@Qualifier("DBDao")
public class DBDaoImpl implements DBDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public String getDBData(String sql) {
try {
if (jdbcTemplate.getDataSource() instanceof org.apache.tomcat.jdbc.pool.DataSource) {
org.apache.tomcat.jdbc.pool.DataSource tcDataSource = (org.apache.tomcat.jdbc.pool.DataSource) jdbcTemplate.getDataSource();
// System.out.println("Schema: " + jdbcTemplate.getDataSource().getConnection().getSchema());
System.out.println("Schema: "+tcDataSource.getUrl());
System.out.println("Schema: "+tcDataSource.getUsername());
}
} catch (Exception ex) {
Logger.getLogger(DBDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
}
// System.out.println("sql: " + sql);
return (String) jdbcTemplate.queryForObject(sql, new Object[]{}, String.class);
//throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
下面是UtilHelper类,它使用导致问题的服务或存储库:
public class UtilHelper {
@Resource(name="DBService")
private DBService DBService;
public HashMap<String,String> getData(String countryName,String module, HashMap queryData)
{
Iterator itr = queryData.entrySet().iterator();
String query=null;
while(itr.hasNext())
{
Map.Entry qEntry = (Map.Entry)itr.next();
query = (String)qEntry.getValue();
// log.debug("Query is -->"+query);
try
{
if(module.equals("COLL")){
if(countryName.equals("Brazil")){
DBContextHolder.setDBName(DBName.COLL_BRAZIL);
}else if(countryName.equals("Italy")){
DBContextHolder.setDBName(DBName.COLL_ITALY);
}else if(countryName.equals("Belgium")){
DBContextHolder.setDBName(DBName.COLL_BELGIUM);
}else if(countryName.equals("Netherlands")){
DBContextHolder.setDBName(DBName.COLL_NETHERLAND);
}else if(countryName.equals("United Kingdom")){
DBContextHolder.setDBName(DBName.COLL_UK);
}else if(countryName.equals("France")){
DBContextHolder.setDBName(DBName.COLL_FRANCE);
}else if(countryName.equals("Germany")){
DBContextHolder.setDBName(DBName.COLL_GERMANY);
}else if(countryName.equals("Austria")){
DBContextHolder.setDBName(DBName.COLL_AUSTRIA);
}else if(countryName.equals("Switzerland")){
DBContextHolder.setDBName(DBName.COLL_SWISS);
}else if(countryName.equals("Chile")){
DBContextHolder.setDBName(DBName.COLL_CHILE);
}else if(countryName.equals("Colombia")){
DBContextHolder.setDBName(DBName.COLL_COLOMBIA);
}
}else if(module.equals("CSS")){
if(countryName.equals("BRAZIL_CSS")) {
DBContextHolder.setDBName(DBName.CSS_BRAZIL);
}else if(countryName.equals("UK_CSS")){
DBContextHolder.setDBName(DBName.CSS_UK);
}else if(countryName.equals("Germany_CSS")){
DBContextHolder.setDBName(DBName.CSS_GERMANY);
}else if(countryName.equals("Chile_CSS")){
DBContextHolder.setDBName(DBName.CSS_CHILE);
}else if(countryName.equals("Colombia_CSS")){
DBContextHolder.setDBName(DBName.CSS_COLOMBIA);
}else if(countryName.equals("Italy_CSS")){
DBContextHolder.setDBName(DBName.CSS_ITALY);
}else if(countryName.equals("France_CSS")){
DBContextHolder.setDBName(DBName.CSS_FRANCE);
}
}
//System.out.println("DBSERVICD Query: "+(query));
//System.out.println("DBSERVICD Country: "+(countryName));
//System.out.println("DBSERVICD Module: "+(module));
System.out.println("DBSERVICD DATA: "+DBService.getDBData(query));
queryData.put(qEntry.getKey(),DBService.getDBData(query));
}
catch(Exception sqle)
{
String error = "Error: "+sqle.getMessage()+"Query: "+query+"\n CONTINUE???";
sqle.printStackTrace();
//connIssuePop(error);
queryData.put(qEntry.getKey(),"No Data");
continue;
}
}
return queryData;
}
}
在上面的代码中,我无法访问应用程序中的服务(DBService)。 每次都是null。但当我试图用
访问它时ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
DBService DBService = (DBService) context.getBean("DBService");
它可以正常使用多个数据库,但是需要花费大量时间来初始化所有bean注册bean并使用shutdown hook。我不想使用它,但是无法在应用程序中仅使用服务或存储库,因为每次它的值都为null。
这是我使用UtilHelper类的SpringBootApplication类。
@SpringBootApplication
public class SpringBootMarshallingApplication implements CommandLineRunner {
@Resource
XmlConverter xmlConverter;
public static void main(String[] args) {
SpringApplication.run(SpringBootMarshallingApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
final String queriesCollxmlFile = "QueriesColl.xml";
final String dbInfoCollxmlFile = "dbInfoColl.xml";
final String queriesCSSxmlFile = "QueriesCSS.xml";
final String dbInfoCSSxmlFile = "dbInfoCSS.xml";
HashMap<String,HashMap> listMap=new HashMap<String,HashMap>();
System.out.println("Convert Xml to Customer Object!");
System.out.println("dbInfoColl.xml>>>>>>>>>>>>!");
dbInfo dbInfoColl = (dbInfo) xmlConverter.convertFromXMLToObject(dbInfoCollxmlFile);
Queries cust2 = (Queries) xmlConverter.convertFromXMLToObject(queriesCollxmlFile);
System.out.println(dbInfoColl.getCountry().size());
for (country country : dbInfoColl.getCountry()) {
UtilHelper util=new UtilHelper();
HashMap queryData= util.genQueries(country, cust2.getQuery());
// util.getData(dbInfoCSSxmlFile, dbInfoCSSxmlFile, queryData);
HashMap data =util.getData(country.getCountryName(),"COLL",queryData);
listMap.put(country.getCountryName(),data);
// System.out.println(country.toString());
}
for (Query query : cust2.getQuery()) {
System.out.print(query.getQueryValue()+" ");
}
System.out.println(" ");
for (Map.Entry<String, HashMap> entry : listMap.entrySet()) {
String countryName = entry.getKey();
HashMap <String,String> value = entry.getValue();
for (Map.Entry<String, String> entry1 : value.entrySet()) {
String key = entry1.getKey();
String value1 = entry1.getValue();
System.out.print(key+":"+value1+" ");
}
System.out.println(" ");
}
for (Query query : cust2.getQuery()) {
for (Map.Entry<String, HashMap> entry : listMap.entrySet()) {
String countryName = entry.getKey();
HashMap <String,String> value = entry.getValue();
for (Map.Entry<String, String> entry1 : value.entrySet()) {
String key = entry1.getKey();
String value1 = entry1.getValue();
if(query.getQueryValue().trim().toUpperCase().equals(key.toString().trim().toUpperCase()))
{
System.out.print(key+":"+value1+" ");
}
}
System.out.println(" ");
}
}
System.out.println("QueriesColl.xml>>>>>>>>>>>>!");
System.out.println(cust2.getQuery().size());
}
}