2017-09-12 394 views
0

我有一個spring.appperties文件和彈簧數據jpa的彈簧啓動應用程序。 在應用程序中我有一個外部依賴項,需要加載具有基於xml配置的外部Spring項目的Bean。 外部xml有它自己的組件掃描和它自己的spring jpa安裝程序來與其他數據庫進行交互,從而在Application.properties文件中給出該數據庫屬性,並且我使用@ImportResources在其父應用程序中注入它的bean。但是當我在做這個spring數據時,jpa沒有加載接口存儲庫bean。彈簧 - 引導配置

問題是它加載了xml的bean,但是fils加載了父項目的存儲庫bean的bean。

Appconfig.class

@Configuration 
@ComponentScan(basePackages = {"com.xx.xx"}) 
public class AppConfig { 

} 

ConfigProperties.class

@Configuration 
@Import({AppConfig.class}) 
@ImportResource("classpath:xx-context.xml") 
public class ConfigProperties { 

} 

XX-context.xml中(樣品)

<mvc:annotation-driven /> 

    <import resource="yy-repository.xml" /> 
    <import resource="classpath:zz-config.xml" /> 

    <context:component-scan base-package="com.yy.yy" /> 

    <bean id="bean_id" 
     //defination 
    </bean> 

    <bean> 
     //defination 
    </bean> 

</beans> 

MainApp.java

@SpringBootApplication 
public class MainApp extends SpringBootServletInitializer { 

異常

應用程序未能啓動

描述:

領域AARepo在com.xx.xx.services需要.impl.yyImpl無法找到類型爲 'com.xx.x.repository.AARepo'的bean。

行動

考慮您的 configuration.`定義類型 'com.xx.xx.repository.AARepo' 的豆

回答

0

它似乎Springboot不能查看您的回購類。你可以嘗試添加: @EnableJpaRepositories("com.xx.x.repository")給你主App類?

--- UPDATE ---

要更新您的MainApp完整的配置。你可以試試這個嗎?!

@SpringBootApplication 
@EnableJpaRepositories("com.xx.x.repository") 
@EntityScan("com.xx.x.repository.entity") 
public class MainApp extends SpringBootServletInitializer { 

希望得到這個幫助。

+0

我還有想這沒有很好的頂端使用@EnableAutoConfiguration。我覺得問題是@ImportResources加載其他依賴項目的組件和數據源,並且因爲它有一個數據源設置,所以它無法爲父項 – user1626001

+0

@ user1626001執行它:只需更新MainApp中的完整配置,你可以試試嗎? –

1

AppConfigClass

-1
@Configuration 
@EnableTransactionManagement(proxyTargetClass = true) 
@EnableJpaRepositories(basePackages = {"com.arcvideo.cloudagent.repo", "com.arcvideo.cloudagent.customer"}) 
@PropertySource("classpath:database.properties") 

public class DatabaseConfig { 

    @Autowired 
    private Environment env; 

    @Bean(destroyMethod = "close") 
    DataSource dataSource() { 
     HikariConfig dataSourceConfig = new HikariConfig(); 
     dataSourceConfig.setPoolName("springHikariCP"); 
     dataSourceConfig.setConnectionTestQuery("SELECT 1"); 
     dataSourceConfig.setDataSourceClassName(env.getProperty("hibernate.connection.driver_class")); 
     Properties dataSourceProperties = new Properties(); 
     dataSourceProperties.put("url", env.getProperty("hibernate.connection.url")); 
     dataSourceProperties.put("user", env.getProperty("hibernate.connection.username")); 
     dataSourceProperties.put("password", env.getProperty("hibernate.connection.password")); 
     dataSourceConfig.setDataSourceProperties(dataSourceProperties); 

     return new HikariDataSource(dataSourceConfig); 
    } 

    @Bean 
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() { 
     LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean(); 
     entityManagerFactory.setDataSource(dataSource()); 
     entityManagerFactory.setPackagesToScan(env.getProperty("entitymanager.packagesToScan")); 

     HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); 
     entityManagerFactory.setJpaVendorAdapter(vendorAdapter); 

     Properties additionalProperties = new Properties(); 
     additionalProperties.put("hibernate.dialect", env.getProperty("hibernate.dialect")); 
     additionalProperties.put("hibernate.cache.use_query_cache", env.getProperty("hibernate.cache.use_query_cache")); 
     additionalProperties.put("hibernate.cache.use_second_level_cache", env.getProperty("hibernate.cache.use_second_level_cache")); 
     additionalProperties.put("hibernate.cache.region.factory_class", env.getProperty("hibernate.cache.region.factory_class")); 
     additionalProperties.put("hibernate.autoReconnect", env.getProperty("hibernate.autoReconnect")); 
     additionalProperties.put("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); 
     additionalProperties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql")); 
     additionalProperties.put("hibernate.format_sql", env.getProperty("hibernate.format_sql")); 
     additionalProperties.put("javax.persistence.sharedCache.mode", env.getProperty("javax.persistence.sharedCache.mode")); 
     additionalProperties.put("hibernate.generate_statistics", env.getProperty("hibernate.generate_statistics")); 
     entityManagerFactory.setJpaProperties(additionalProperties); 

     return entityManagerFactory; 
    } 
}