带有 TaskListener @Autowired 问题的 Alfresco Process Services
Alfresco Process Services with TaskListener @Autowired issue
我正在使用 Alfresco Process Services 并为 TaskListeners 和 Delegations[=22= 等自定义逻辑创建了一个 spring 引导项目].我正在从这个 Maven 项目创建 jar 文件并将其复制到 webapps/activiti-app/WEB-INF/lib 文件夹中。
我有一个简单的 TaskListener,如下所示,它在任务启动时被调用。但是 @Autowired 变量总是 null。
package com.activiti.extension.bean;
@Component("myTaskListener")
public class MyTaskListener implements TaskListener {
@Autowired
UserService userService;
@Override
public void notify(DelegateTask task) {
logger.info("userService: " +userService); // Always prints null
}
这是因为您的 MyTaskListener
在初始化期间被注释为 @Component 或至少被 spring 忽略。对于 auto-wiring 功能 spring 需要在提供的 @ComponentScan 包下使用此注解(或类似注解)将 class 视为一个 bean,否则它将作为正常 java class 因此 @autowired
在你的情况下没有用。
我终于成功了。我将任务侦听器放在具有完整包名称的任务属性的 class 字段中。现在我将 Delegate 表达式放入 ${myTaskListener} 并且它有效...
感谢大家的宝贵时间和帮助
下面的代码适合我
@Component
public class MyTaskListener implements TaskListener {
public static UserService getUserServiceObject() {
return SpringApplicationContextHolder.getApplicationContext().getBean(UserService.class);
}
@Override
public void notify(DelegateTask delegateTask) {
//UserService Object, It is not null now
getUserServiceObject();
}
}
@Component
public class SpringApplicationContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
还有一种方法可以使用 Alfresco Spring 应用程序上下文.
获取自定义服务 "UserService"
首先访问 ServiceRegistry(用于访问 Alfresco 服务以及任何其他自定义服务的注册表):
ServiceRegistry serviceRegistry = (ServiceRegistry) Context.getProcessEngineConfiguration().getBeans().get(ActivitiConstants.SERVICE_REGISTRY_BEAN_KEY);
然后获取自定义服务UserService:
QName qname = QName.createQName("UserService");
UserService userService = (UserService) serviceRegistry.getService(qname);
我正在使用 Alfresco Process Services 并为 TaskListeners 和 Delegations[=22= 等自定义逻辑创建了一个 spring 引导项目].我正在从这个 Maven 项目创建 jar 文件并将其复制到 webapps/activiti-app/WEB-INF/lib 文件夹中。
我有一个简单的 TaskListener,如下所示,它在任务启动时被调用。但是 @Autowired 变量总是 null。
package com.activiti.extension.bean;
@Component("myTaskListener")
public class MyTaskListener implements TaskListener {
@Autowired
UserService userService;
@Override
public void notify(DelegateTask task) {
logger.info("userService: " +userService); // Always prints null
}
这是因为您的 MyTaskListener
在初始化期间被注释为 @Component 或至少被 spring 忽略。对于 auto-wiring 功能 spring 需要在提供的 @ComponentScan 包下使用此注解(或类似注解)将 class 视为一个 bean,否则它将作为正常 java class 因此 @autowired
在你的情况下没有用。
我终于成功了。我将任务侦听器放在具有完整包名称的任务属性的 class 字段中。现在我将 Delegate 表达式放入 ${myTaskListener} 并且它有效...
感谢大家的宝贵时间和帮助
下面的代码适合我
@Component
public class MyTaskListener implements TaskListener {
public static UserService getUserServiceObject() {
return SpringApplicationContextHolder.getApplicationContext().getBean(UserService.class);
}
@Override
public void notify(DelegateTask delegateTask) {
//UserService Object, It is not null now
getUserServiceObject();
}
}
@Component
public class SpringApplicationContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext = null;
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
还有一种方法可以使用 Alfresco Spring 应用程序上下文.
获取自定义服务 "UserService"首先访问 ServiceRegistry(用于访问 Alfresco 服务以及任何其他自定义服务的注册表):
ServiceRegistry serviceRegistry = (ServiceRegistry) Context.getProcessEngineConfiguration().getBeans().get(ActivitiConstants.SERVICE_REGISTRY_BEAN_KEY);
然后获取自定义服务UserService:
QName qname = QName.createQName("UserService");
UserService userService = (UserService) serviceRegistry.getService(qname);