Spring bean注入问题在Flowable服务任务中

huangapple go评论92阅读模式
英文:

Spring bean injection problem in Flowable service task

问题

我有一个关于在Flowable的服务任务中进行Spring Bean注入的问题,为什么只有这种带有静态修饰符的注入方式有效,它的逻辑是什么?

我必须在Flowable的Java服务任务中注入一个Spring Bean,并且我尝试了一些不同种类的注入方式,包括字段注入、构造函数注入和Setter注入,最终我发现带有静态修饰符的Setter注入对我有效,就像这样:

  1. public class GetCurrentUserDlg implements JavaDelegate {
  2. private static PersonService personService;
  3. @Autowired
  4. public void setPersonService(PersonService personService) {
  5. this.personService = personService;
  6. }
  7. @Override
  8. public void execute(DelegateExecution execution) {
  9. personService.getCurrentUser();
  10. }
  11. }
英文:

I have a question about spring bean injection in service tasks of Flowable, why only this kind of injection with a static modifier worked, and what is the logic of it?

I must inject a spring bean in a Flowable java service task, and I tested some different kind of injection Field, constructor, and setter injection, eventually setter injection with static modifier worked for me like this :

  1. public class GetCurrentUserDlg implements JavaDelegate {
  2. private static PersonService personService;
  3. @Autowired
  4. public void setPersonService(PersonService personService) {
  5. this.personService = personService;
  6. }
  7. @Override
  8. public void execute(DelegateExecution execution) {
  9. personService.getCurrentUser();
  10. }
  11. }

答案1

得分: 6

以下是翻译好的部分:

"While I can not answer your question, the following works fine for me:"(虽然我无法回答你的问题,但以下内容对我来说有效:)

  1. public class SomeDelegate implements JavaDelegate {
  2. @Autowired
  3. private SomeBean bean;
  4. @Override
  5. public void execute(DelegateExecution execution) {
  6. System.out.println(this.bean);
  7. }
  8. }

"The class is then used in the process via flowable:class="packages.SomeDelegate""(然后该类通过 flowable:class="packages.SomeDelegate" 在流程中使用。)

"But, be aware, that you may have problems with autowiring dependencies in the SomeBean bean. This dependencies are not injected when using the flowable:class attribute. In order for this to work you have to make the SomeDelegate a actual bean itself (e.g. via @Service) and use it in your process definition via flowable:delegateExpression="${someDelegate}""(但请注意,您可能会在SomeBean bean中自动装配依赖项时遇到问题。当使用flowable:class属性时,这些依赖项不会被注入。为了使其工作,您必须将SomeDelegate 自身变成一个实际的bean(例如通过 @Service),并在流程定义中使用它,方法是通过 flowable:delegateExpression="${someDelegate}"。)

"Example:"(示例:)

  1. @Service("someDelegate")
  2. public class SomeDelegate implements JavaDelegate {
  3. ...

  1. <serviceTask id="doSomething" name="Do Something" flowable:delegateExpression="${someDelegate}"/>

(请注意,翻译中的 " 是HTML实体,表示双引号。)

英文:

While I can not answer your question, the following works fine for me:

  1. public class SomeDelegate implements JavaDelegate {
  2. @Autowired
  3. private SomeBean bean;
  4. @Override
  5. public void execute(DelegateExecution execution) {
  6. System.out.println(this.bean);
  7. }
  8. }

The class is then used in the process via flowable:class="packages.SomeDelegate"

But, be aware, that you may have problems with autowiring dependencies in the SomeBean bean. This dependencies are not injected when using the flowable:class attribute. In order for this to work you have to make the SomeDelegate a actual bean itself (e.g. via @Service) and use it in your process definition via flowable:delegateExpression="${someDelegate}"

Example:

  1. @Service("someDelegate")
  2. public class SomeDelegate implements JavaDelegate {
  3. ...

and

  1. <serviceTask id="doSomething" name="Do Something" flowable:delegateExpression="${someDelegate}"/>

答案2

得分: 0

应该像这样工作:

  1. @Component
  2. public class GetCurrentUserDlg implements JavaDelegate {
  3. @Autowired
  4. private PersonService personService;
  5. @Override
  6. public void execute(DelegateExecution execution) {
  7. personService.getCurrentUser();
  8. }
  9. }
  10. @Component
  11. public class PersonService {
  12. // 它的方法
  13. }
英文:

It should work like this:

  1. @Component
  2. public class GetCurrentUserDlg implements JavaDelegate {
  3. @Autowired
  4. private PersonService personService;
  5. @Override
  6. public void execute(DelegateExecution execution) {
  7. personService.getCurrentUser();
  8. }
  9. }
  10. @Component
  11. public class PersonService {
  12. // its methods
  13. }

huangapple
  • 本文由 发表于 2020年8月4日 18:32:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63245025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定