2011-12-22 130 views
13

我想在一個小的獨立應用程序中一起使用彈簧數據和彈簧配置。通過Spring配置掃描Spring數據存儲庫?

... 
    public static void main(String[] args) 
    {   
    ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class); 
    ... 
    } 

我的問題是如何發現春天的數據存儲庫,而無需使用

<jpa:repositories base-package="foo.repositories" /> 

彈簧的配置?

2.如果不是,我可以以某種方式一起使用'ClassPathXmlApplicationContext'和'AnnotationConfigApplicationContext'嗎?

回答

2

此答案現在已過時。

目前還沒有<jpa:repositories … />的等價物。感覺自由的跟蹤JIRA ticket。這個特性將是即將發佈的JPA模塊(1.1)以及MongoDB(1.1)版本的主要特性。

+0

感謝您的快速回復。 – cscsaba 2011-12-23 08:43:51

1

我想你應該看看上下文:組件掃描

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

它會自動檢測與@存儲庫/ @服務/ @組件註釋組件。請檢查here

+0

使用上@Repository註解 '公共接口庫' 接口屬於彈簧數據上下文。無論如何感謝您的幫助和時間。 – cscsaba 2011-12-23 08:41:01

4

爲了完整起見,解決您的第二個問題:是的,您可以結合使用Java和XML配置。這樣你就不必等待下一個Spring Data JPA的發佈。

只是標註您的配置類ImportResource,就像這樣:

@Configuration 
@ImportResource("classpath:jpa-config.xml") 
public class AppConfig { 
    ... 
} 
52

您現在可以使用註釋@EnableJpaRepositories("some.root.package")

例如:

@Configuration 
@EnableTransactionManagement(proxyTargetClass = true) 
@EnableJpaRepositories("some.root.package") 
@ComponentScan(basePackages = { "maybe.another.root.package" }) 
public class SystemConfiguration { 
    ... 
} 

Spring Data's announcement

+0

這是現在正確的答案。謝謝您的幫助! – cspin 2014-05-10 23:50:34