2016-05-17 4 views
1

SpringBootアプリケーションで2つのデータソースを使用しようとしていて、2番目のデータソースをautowireにすることができません。私は多くのことを試してみましたが、これは、私が来た最も近い:SpringBootとSpringJDBCの複数のデータソース

私のYAMLファイル:

spring: 
    first-datasource: 
    url: MyURLString1 
    username: User 
    password: Password 
    driver-class-name: oracle.jdbc.OracleDriver 
    second-datasource: 
    url: MyURLString2 
    username: User 
    password: Password 
    driver-class-name: oracle.jdbc.OracleDriver 

マイアプリケーションクラス:

@SpringBootApplication 
public class MyApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(MyApplication.class, args); 
    } 

    @Bean 
    @Primary 
    @ConfigurationProperties(prefix = "spring.first-datasource") 
    public DataSource firstDataSource() { 
     return DataSourceBuilder.create().build(); 
    } 

    @Bean 
    @ConfigurationProperties(prefix = "spring.second-datasource") 
    public DataSource secondDataSource() { 
     return DataSourceBuilder.create().build(); 
    } 
} 

そして最後に、私のDAO:

@Repository 
public class MyDao { 
    private static final String FIRST_SELECT = "select * from SomeTableInDB1"; 
    private static final String SECOND_SELECT = "select * from AnotherTableInDB2"; 

    @Autowired 
    private JdbcTemplate firstJdbcTemplate; 
    @Autowired 
    @Qualifier("secondDataSource") 
    private JdbcTemplate secondJdbcTemplate; 

    List<DB1Entity> getDB1Entity(Long id) { 
     return firstJdbcTemplate.query(FIRST_SELECT, new Object[] {id}, new BeanPropertyRowMapper(DB1Entity.class)); 
    } 

    List<DB2Entity> getDB2Entity(Long id) { 
     return secondJdbcTemplate.query(SECOND_SELECT, new Object[] {id}, new BeanPropertyRowMapper(DB2Entity.class)); 
    } 
} 

これまでに私が一番近かったのはこれです。 @Qualifierを削除すると、DAOメソッドの両方が実際に動作し、SECOND_SELECT文が自分のDB1に対して有効なSQLであると仮定しているため、最も近いと言います。一度私の非プライマリデータソースの@Qualifierを入れると、SpringはJdbcTemplateオブジェクトではなくDatasouceオブジェクトを期待しているので、私はautowireエラーを受け取ります。プライマリデータソースで動作するので、それは私には変です。ここで

は私のエラーです:

はフィールドautowireませんでした:プライベートorg.springframework.jdbc.core.JdbcTemplate org.my.classpath.secondJdbcTemplateを。ネストされた例外はorg.springframework.beans.factory.NoSuchBeanDefinitionExceptionです:タイプ[org.springframework.jdbc.core.JdbcTemplate]の適格なBeanが依存関係に見つかりませんでした:この依存関係のautowire候補となる少なくとも1つのbeanが必要です。依存関係の注釈:{@ org.springframework.beans.factory.annotation.Autowired(必須= true)、@ org.springframework.beans.factory.annotation.Qualifier(値= secondDataSource)}

+1

DataSourceタイプのBeanを作成しますが、Autowire JdbcTemplateを作成します。あなたはおそらくこのようなものを持っているはずです。 'private JdbcTemplate jdbcTemplate1; プライベートJdbcTemplate jdbcTemplate2; @Autowired @Qualifier( "firstDataSource") パブリックvoid setDataSource(DataSource dataSource){ this.jdbcTemplate1 = new JdbcTemplate(dataSource); } @Autowired @Qualifier( "secondDataSource") 公共ボイドsetDataSource(データソースデータソース){ this.jdbcTemplate2 =新しいJdbcTemplate(データソース)。 } ' – lenach87

+0

私はこれとほぼ同じものを試しましたが、nullのURLについてのエラーが発生しました。私はあなたのコードをコピーし、それが働いたので何かミスタイプする必要があります。ありがとう。私はそれが何かばかげたと確信しています。 – Gremash

+0

@ lenach87これは正しい解決策であり、私はその質問が良いと思います。私はそれを正しいものとしてマークします。 – Gremash

答えて

1

タイプのBean DataSourceですが、試してみてくださいAutowire JdbcTemplateはミスマッチです。あなたはおそらく持つべき理想的には、この

private JdbcTemplate jdbcTemplate1; 
private JdbcTemplate jdbcTemplate2; 

@Autowired 
@Qualifier("firstDataSource") 
public void setDataSource(DataSource dataSource){ 
    this.jdbcTemplate1=new JdbcTemplate(dataSource); 
} 

@Autowired 
@Qualifier("secondDataSource") 
public void setDataSource(DataSource dataSource){ 
    this.jdbcTemplate2=new JdbcTemplate(dataSource); 
} 
0

のようなものではなく、任務は、データソースの一つが動作するように注釈を経由して、デフォルトの配線のほとんどのPRIMARYをマークする必要があります。また、データソースごとにTransactionManagersを個別に作成する必要があります。そうしないと、Springはトランザクションの実行方法を知らないでしょう。以下は、これが

@Primary 
@Bean(name = "dataSource") 
@ConfigurationProperties(prefix="datasource.mysql") 
public DataSource dataSource() { 
    return DataSourceBuilder.create().build(); 
} 

@Primary 
@Bean(name = "transactionManager") 
public DataSourceTransactionManager transactionManager(@Qualifier("dataSource") DataSource dataSource) { 
    return new DataSourceTransactionManager(); 
} 

@Bean(name = "postGresDataSource") 
@ConfigurationProperties(prefix="datasource.postgres") 
public DataSource postgresDataSource() { 
    return DataSourceBuilder.create().build(); 
} 

@Bean(name = "postGresTransactionManager") 
public DataSourceTransactionManager transactionManager(@Qualifier("postGresDataSource") DataSource dataSource) { 
    return new DataSourceTransactionManager(); 
} 

@Transactional(transactionManager="postGresTransactionManager") 
public void createCustomer(Customer cust) { 
    customerDAO.create(cust); 
} 

// specifying a transactionManager attribute is optional if we 
// want to use the default transactionManager since we 
// already marked one of the TM above with @Primary 
@Transactional 
public void createOrder(Order order) { 
    orderDAO.create(order); 
} 

この情報がお役に立てば幸いに行われるべき方法の完全な例です。

0

ここも数日間、私を混同他人-動作していない "状況を提供します。Springbootアプリケーション内の同じタイプの2つのデータソースをを、設定すると、期待どおり

@Qualifierは右豆を拾うために動作しません。 。 Springフレームワークで認識されないように動作します。 注釈を含む@SpringbootApplication注釈を使用する理由は、Springbootでユーザに提供するデータソースを自動設定するためです。

これは、@Qualifierの動作に重大な影響を与えます。

関連する問題