[英]ClassBridge with DAO class injected
我有一個Hibernate Search ClassBridge
,我想在其中使用@Inject
注入Spring 4.1托管的DAO / Service類。 我已經用@Configurable
注釋了ClassBridge
。 我注意到Spring 4.2添加了一些其他的生命周期方法來解決這個問題,但我使用的是Spring 4.1
其目的是根據查詢結果將自定義字段存儲到索引文檔中。
但是,由於DAO依賴於SessionFactory
初始化,因此不會注入它,因為在處理@Configurable
bean時它尚不存在。
關於如何實現這一目標的任何建議?
Hibernate Search 5.8.0包括對bean注入的支持。 您可以看到問題https://hibernate.atlassian.net/browse/HSEARCH-1316 。
但是,我無法使其在我的應用程序中正常工作,並且已經實現了解決方法。
我創建了一個應用程序上下文提供程序來獲取Spring應用程序上下文。
public class ApplicationContextProvider implements ApplicationContextAware {
private static ApplicationContext context;
public static ApplicationContext getApplicationContext() {
return context;
}
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
ApplicationContextProvider.context = context;
}
}
我已將其添加到配置類。
@Configuration
public class RootConfig {
@Bean
public ApplicationContextProvider applicationContextProvider() {
return new ApplicationContextProvider();
}
}
最終,我在橋中使用了它來檢索彈簧豆。
public class AttachmentTikaBridge extends TikaBridge {
@Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
// get service bean from the application context provider (to be replaced when HS bridges support beans injection)
ApplicationContext applicationContext = ApplicationContextProvider.getApplicationContext();
ExampleService exampleService = applicationContext.getBean(ExampleService .class);
// use exampleService ...
super.set(name, content, document, luceneOptions);
}
}
我認為這種解決方案與其他解決方案相比非常簡單,並且除了在運行時進行bean注入之外,沒有任何大的副作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.