2014-07-06 14 views
0

我正在尋找使用Spring Boot和Spring Data(RELEASE 1.1.3)圍繞靜態數據源(從第三方獲得)構建應用程序,該應用程序可以有效地提供API數據,我可以從中構建一個前端,但也可以作爲依賴項包含在其他項目中(以提供對靜態數據的訪問)我已經設法使用@RepositoryRestResource註釋來實現API部分,但我一直在努力後者。如何跨項目配置多個數據源

實施例類: 單項實體

@Entity 
@Table(name = "invTypes") 
public class Item { 


    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "typeID", nullable = false) 
    private int id; 

    @Column(name = "typeName") 
    private String name; 

    //Remainder ommitted 
} 

ItemDao

@RepositoryRestResource(collectionResourceRel = "items", path = "items") 
public interface ItemDao extends PagingAndSortingRepository<Item, Integer> { 

    Item findById(@Param("id") int id); 

    Item findByName(@Param("name") String name); 

    List<Item> findByNameContains(@Param("name") String name); 
} 

基本上我想打包本作中另一個項目中使用的罐,使得我可以配置2個數據庫(一個是靜態數據源,另一個與新項目相關)

我已經設法打包靜態數據項目,並將其作爲依賴項包含在另一個pom.xml中,但由於新項目也使用數據庫,因此導入的靜態數據jar默認爲新項目中配置的任何數據庫。這將導致一個錯誤,如:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxException: Table 'new_project.invtypes' doesn't exist 

(這是因爲新的項目配置爲使用「NEW_PROJECT」數據庫,以及被配置爲使用Hibernate的ImprovedNamingStrategy)

一位同事提到的有關配置這兩個數據庫在新項目的application.properties中 - 但我沒有很好的運用這種方法。我已經嘗試更改spring中拾取的靜態項目的數據源屬性的前綴,以便可以配置兩個數據庫(使用此指南:http://xantorohara.blogspot.co.uk/2013/11/spring-boot-jdbc-with-multiple.html),但是似乎兩個項目都使用相同的數據源(My道歉,如果我有這一切都錯了)

的靜態項目的配置則成了:

@Configuration 
@ConfigurationProperties(prefix = "spring.ds_static") 
class DataSourceConfig extends TomcatDataSourceConfiguration { 
    @Bean(name = "dsStatic") 
    public DataSource dataSource() { 
    return super.dataSource(); 
    } 

    @Bean(name = "jdbcStatic") 
    public JdbcTemplate jdbcTemplate(DataSource dsStatic) { 
    return new JdbcTemplate(dsStatic); 
    } 
} 

以上最終成爲該方法的性能,但導致相同的錯誤面前:

# Configured properties for the new project 
spring.datasource.url=jdbc:mysql://localhost/new_projct 
spring.datasource.username=dbuser 
spring.datasource.password=password 
spring.datasource.driverClassName=com.mysql.jdbc.Driver 
spring.jpa.show-sql=true 
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy 

# Configured properties for the static data jar 
spring.ds_static.url=jdbc:mysql://localhost/static_data 
spring.ds_static.username=dbuser 
spring.ds_static.password=password 
spring.ds_static.driverClassName=com.mysql.jdbc.Driver 

如何包含和配置一個jar來連接到它自己的數據庫,而不是包含它作爲依賴項目的項目的數據庫?我是否需要在靜態數據項目中執行一些配置來強制它連接到我決定的數據源?

感謝您的幫助,如果你需要額外的信息,我很樂意爲其提供

+0

這並不是真的很清楚你想要什麼,你做了什麼,或者當你嘗試它時出了什麼問題。在最終的複合應用程序中是否需要2個DataSources,或者只有一個? –

+0

嗨,我試着用更多的信息來更新它,並且讓它更加清晰我試圖實現的目標 – spjthe

+0

這兩個項目都使用JPA嗎?你可以爲它們顯示DataSource配置嗎? –

回答

0

看起來你需要在「靜態」罐子的自定義實體管理器工廠(如見docs here),以及作爲您的自定義數據源。在擁有自定義實體管理器工廠後,您需要聲明@EnableJpaRepositories並明確引用實體管理器因素的bean ID。

相關問題