在 Liferay 索引器中使用 Spring 自动装配服务 类
Using Spring autowired Service classes within a Liferay Indexer
我在我的 liferay portlet 中使用 Spring @Service 类 来获取和存储数据。它们是使用 @autowired 注释注入的。一切都按预期工作。当我尝试在 Liferay BaseIndexer 子类中使用相同的方法(将数据放入搜索引擎中)时,@autowired 注释 类 全部为空(未注入)。
有没有办法在索引器中获取这些服务类?
此致,
丹尼尔
此索引器未由 Spring 实例化,因此您将无法自动装配您的服务。
但是,您可以实现自定义 ApplicationContextProvider(实现 Spring ApplicationContextAware)并使用它来注入您的服务。应该很简单。
你应该开始创建这个 class,然后让 Spring 发现它(确保这个 class 被 spring 扫描):
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* Created by Alberto Martínez Ballesteros on 18/03/16.
*/
@Component("applicationContextProvider")
public class ApplicationContextProvider implements ApplicationContextAware {
private static ApplicationContext context = null;
public static ApplicationContext getApplicationContext() {
return context;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
}
然后,您可以使用此 ApplicationContextProvider 在索引器中注入您的服务 class:
例如:
public class CategoryIndexer extends BaseIndexer {
private CategoryService categoryService;
[....]
@Override
protected void doReindex(String className, long classPK) throws Exception {
if (categoryService == null) {
initService();
}
final Category category = categoryService.get(classPK);
doReindex(category);
}
private void initService() {
categoryService = (CategoryService) ApplicationContextProvider.getApplicationContext()
.getBean("categoryService");
}
[....]
如您所见,您不能以这种方式使用@Autowired,但无论如何您都可以注入您的bean。
此致。
我在我的 liferay portlet 中使用 Spring @Service 类 来获取和存储数据。它们是使用 @autowired 注释注入的。一切都按预期工作。当我尝试在 Liferay BaseIndexer 子类中使用相同的方法(将数据放入搜索引擎中)时,@autowired 注释 类 全部为空(未注入)。
有没有办法在索引器中获取这些服务类?
此致,
丹尼尔
此索引器未由 Spring 实例化,因此您将无法自动装配您的服务。
但是,您可以实现自定义 ApplicationContextProvider(实现 Spring ApplicationContextAware)并使用它来注入您的服务。应该很简单。
你应该开始创建这个 class,然后让 Spring 发现它(确保这个 class 被 spring 扫描):
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* Created by Alberto Martínez Ballesteros on 18/03/16.
*/
@Component("applicationContextProvider")
public class ApplicationContextProvider implements ApplicationContextAware {
private static ApplicationContext context = null;
public static ApplicationContext getApplicationContext() {
return context;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
}
然后,您可以使用此 ApplicationContextProvider 在索引器中注入您的服务 class:
例如:
public class CategoryIndexer extends BaseIndexer {
private CategoryService categoryService;
[....]
@Override
protected void doReindex(String className, long classPK) throws Exception {
if (categoryService == null) {
initService();
}
final Category category = categoryService.get(classPK);
doReindex(category);
}
private void initService() {
categoryService = (CategoryService) ApplicationContextProvider.getApplicationContext()
.getBean("categoryService");
}
[....]
如您所见,您不能以这种方式使用@Autowired,但无论如何您都可以注入您的bean。
此致。