[英]How to use Spring managed beans in Drool
我有一個由 spring 管理的服務,我想在流口水規則中使用或注入作為依賴項。 如何在 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 實例。 我們可以假設@Service
和這個@Component
都被組件掃描選中。
然后,您可以在規則中以與處理任何其他數據相同的方式與它進行交互:
dialect "mvel"
rule "something"
when
service: SomeService()
then
service.doSomething()
end
請記住service: SomeService()
匹配工作內存中 SomeService 的一個實例,並將其分配給service
變量以在規則中使用。 它不新了一個新的實例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.