前提・実現したいこと
Springで一つのデータベースから情報を持ってくることは出来ているのですが、複数のデータベースを指定するとエラーが出ます。
データベースの参照は、(データベース名)Config.javaというファイル内に記述しており、@Beanもこの中で記述しています。
@Configurationというアノテーションを付与しており、これを外せば付いている方のデータベースを参照して、外した方のファイルは読み込まれないようになります。
また、エラーは実際に処理させたいタイミングではなく、アプリケーションを起動する時に発生しています。
同じアプリケーション内で複数データベースを同時に参照したいため、@Configurationをどちらにも付与している状態で処理したいです。
@ConfigurationProperties(prefix = "config.hoge")というアノテーションを付与したり、
createDtaSourceで作成されるものの名称を変えてみるといった対処をしてみたのですが、解決できませんでした。
対処法をご存知の方いらっしゃいましたら、ご教示よろしくお願いいたします。
発生している問題・エラーメッセージ
Parameter 0 of constructor in com.example.sample.repository.hoge.HogeDaoImpl required a single bean, but 2 were found: - hogeConfig: defined in file [/Users/****/sample/target/classes/com/example/sample/config/HogeConfig.class] - fugaConfig: defined in file [/Users/****/sample/target/classes/com/example/sample/config/FugaConfig.class] Action: Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
HogeConfig.class
java
1package com.example.sample.config; 2 3import javax.sql.DataSource; 4import org.seasar.doma.jdbc.Config; 5import org.seasar.doma.jdbc.JdbcLogger; 6import org.seasar.doma.jdbc.SimpleDataSource; 7import org.seasar.doma.jdbc.dialect.Dialect; 8import org.seasar.doma.jdbc.dialect.MysqlDialect; 9import org.springframework.context.annotation.Bean; 10import org.springframework.context.annotation.Configuration; 11import org.springframework.jdbc.datasource.DataSourceTransactionManager; 12import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; 13import org.springframework.transaction.PlatformTransactionManager; 14import org.springframework.transaction.annotation.EnableTransactionManagement; 15 16@Configuration 17@EnableTransactionManagement 18public class HogeConfig implements Config { 19 private DataSource dataSource = createDataSource(); 20 21 private Dialect dialect = new MysqlDialect(); 22 23 @Override 24 public DataSource getDataSource() { 25 // TODO Auto-generated method stub 26 return dataSource; 27 } 28 29 @Override 30 public Dialect getDialect() { 31 // TODO Auto-generated method stub 32 return dialect; 33 } 34 35 @Bean 36 public PlatformTransactionManager transactionManager() { 37 return new DataSourceTransactionManager(dataSource); 38 } 39 40 protected static DataSource createDataSource() { 41 SimpleDataSource dataSource = new SimpleDataSource(); 42 dataSource.setUrl("jdbc:postgresql://localhost:1234/hoge"); 43 dataSource.setUser("******"); 44 dataSource.setPassword("******"); 45 return new TransactionAwareDataSourceProxy(dataSource); 46 } 47} 48 49
FugaConfig.class
java
1package com.example.sample.config; 2 3import javax.sql.DataSource; 4import org.seasar.doma.jdbc.Config; 5import org.seasar.doma.jdbc.JdbcLogger; 6import org.seasar.doma.jdbc.SimpleDataSource; 7import org.seasar.doma.jdbc.dialect.Dialect; 8import org.seasar.doma.jdbc.dialect.MysqlDialect; 9import org.springframework.context.annotation.Bean; 10import org.springframework.context.annotation.Configuration; 11import org.springframework.jdbc.datasource.DataSourceTransactionManager; 12import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy; 13import org.springframework.transaction.PlatformTransactionManager; 14import org.springframework.transaction.annotation.EnableTransactionManagement; 15 16@Configuration 17@EnableTransactionManagement 18public class FugaConfig implements Config { 19 private DataSource dataSource = createDataSource(); 20 21 private Dialect dialect = new MysqlDialect(); 22 23 @Override 24 public DataSource getDataSource() { 25 // TODO Auto-generated method stub 26 return dataSource; 27 } 28 29 @Override 30 public Dialect getDialect() { 31 // TODO Auto-generated method stub 32 return dialect; 33 } 34 35 @Bean 36 public PlatformTransactionManager transactionManager() { 37 return new DataSourceTransactionManager(dataSource); 38 } 39 40 protected static DataSource createDataSource() { 41 SimpleDataSource dataSource = new SimpleDataSource(); 42 dataSource.setUrl("jdbc:postgresql://localhost:1234/fuga"); 43 dataSource.setUser("******"); 44 dataSource.setPassword("******"); 45 return new TransactionAwareDataSourceProxy(dataSource); 46 } 47} 48 49
補足情報(FW/ツールのバージョンなど)
Spring Tool Suit 4
Postgres
Mac OS
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/07 12:32
2018/12/10 00:47
2018/12/10 11:59
2018/12/11 04:48