Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Drool中使用Spring管理的bean_Spring_Spring Boot_Drools - Fatal编程技术网

如何在Drool中使用Spring管理的bean

如何在Drool中使用Spring管理的bean,spring,spring-boot,drools,Spring,Spring Boot,Drools,我有一个由spring管理的服务,我想在drool规则中使用或注入作为依赖项。如何在Drool规则中使用服务bean @Service public class SomeService { public void doSomething() {} } dialect "mvel" rule "something" when ................ then service.doS

我有一个由spring管理的服务,我想在drool规则中使用或注入作为依赖项。如何在Drool规则中使用服务bean

@Service 
public class SomeService {

     public void doSomething() {}
}



dialect "mvel"

rule "something"
    when
       ................
    then 
       service.doSomething()
end

当您调用bean实例时,将其传递到工作内存中,就像传递任何其他数据一样。通过这种方式,您可以自动连线或以其他方式利用依赖项注入来获得Spring管理的单例bean,然后在规则中使用相同的bean,而无需重新创建另一个实例

@Component
class RuleExecutor {

  private final SomeService service; // Bean set via constructor injection

  public RuleExecutor(SomeService service) {
    this.service = service;
  }

  public void fireRules( ... ) { // assuming other data passed in via parameter
    KieSession kieSession = ...; 
    kieSession.insert( this.service); // insert bean into the working memory
    kieSession.insert( ... );  // insert other data into working memory
    kieSession.fireAllRules();
  }
}
在这个例子中,我使用构造函数注入来传递bean实例。我们可以假设
@服务
和该
@组件
都是通过组件扫描获取的

然后,您可以按照与任何其他数据相同的方式在规则中与它交互:

dialect "mvel"

rule "something"
when
  service: SomeService()
then 
  service.doSomething()
end
请记住,
service:SomeService()
匹配工作内存中SomeService的实例,并将其分配给
service
变量以在规则中使用。它不会创建新实例

@Component
class RuleExecutor {

  private final SomeService service; // Bean set via constructor injection

  public RuleExecutor(SomeService service) {
    this.service = service;
  }

  public void fireRules( ... ) { // assuming other data passed in via parameter
    KieSession kieSession = ...; 
    kieSession.insert( this.service); // insert bean into the working memory
    kieSession.insert( ... );  // insert other data into working memory
    kieSession.fireAllRules();
  }
}