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

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

Spring bean injection problem in Flowable service task

问题

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

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

public class GetCurrentUserDlg implements JavaDelegate {

    private static PersonService personService;

    @Autowired
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }

    @Override
    public void execute(DelegateExecution execution) {
        personService.getCurrentUser();
    }
}
英文:

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 :

public class GetCurrentUserDlg implements JavaDelegate {

    private static PersonService personService;

    @Autowired
    public void setPersonService(PersonService personService) {
        this.personService = personService;
    }

    @Override
    public void execute(DelegateExecution execution) {
        personService.getCurrentUser();
    }
}

答案1

得分: 6

以下是翻译好的部分:

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

public class SomeDelegate implements JavaDelegate {

    @Autowired
    private SomeBean bean;

    @Override
    public void execute(DelegateExecution execution) {
        System.out.println(this.bean);
    }
}

"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:"(示例:)

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

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

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

英文:

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

public class SomeDelegate implements JavaDelegate {

    @Autowired
    private SomeBean bean;

    @Override
    public void execute(DelegateExecution execution) {
        System.out.println(this.bean);
    }
}

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:

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

and

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

答案2

得分: 0

应该像这样工作:

@Component
public class GetCurrentUserDlg implements JavaDelegate {

    @Autowired
    private PersonService personService;

    @Override
    public void execute(DelegateExecution execution) {
        personService.getCurrentUser();
    }
}

@Component
public class PersonService {
   // 它的方法
}
英文:

It should work like this:

@Component
public class GetCurrentUserDlg implements JavaDelegate {

    @Autowired
    private PersonService personService;

    @Override
    public void execute(DelegateExecution execution) {
        personService.getCurrentUser();
    }
}

@Component
public class PersonService {
   // its methods
}

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:

确定