我的問題是可以用Drools,Mule等來實現嗎?
這是一個相當開放的問題,可能比SO更適合程序員。看看very good answers to this similar question。
我的一個想法是使用Spring並使用 applicationContext.getBean(「beanName」+ user.getkey())。這將使 允許在業務邏輯中可能出現差異 的位置動態加載bean。這裏唯一不好的地方是主要的 代碼與Spring相關,我真的不認爲這是一個很大的 交易。
您可以使邏輯可插入,而無需自己綁定到Spring。使用applicationContext.getBean()
失敗了依賴注入的目的,所以我不建議這樣做。嘗試是這樣的:
- 定義封裝了用戶特定的邏輯
- 創建爲每種類型的用戶
- 的Autowire接口的所有實現成任何bean需要使用它們的接口實現的接口。
- 需要時,根據用戶的類型選擇實現。
我也不知道框架非常好,但可以在 使用一些建議採取哪一種途徑。
首先隱藏接口背後的邏輯並將用戶特定的東西封裝到不同的類中。一旦所有東西都依賴於接口,如果以後需要將用戶特定的東西移動到規則引擎,那麼在不破壞應用程序的其餘部分的情況下,應該很容易。
代碼示例:
public enum UserType {
FOO, BAR
}
public interface UserStrategy {
public void doStuff();
public UserType getUserType();
}
@Component
public class FooUserStrategy implements UserStrategy {
public void doStuff() {
// foo specific stuff
}
public UserType getUserType() {
return UserType.FOO;
}
}
@Component
public BarUserStrategy implements UserStrategy {
public void doStuff() {
// bar specific stuff
}
public UserType getUserType() {
return UserType.BAR;
}
}
@Service
public class ClassThatUsesStrategies {
private Map<UserType, UserStrategy> map = new HashMap<>();
public void doUserTypeSpecificStuffForUser(User user) {
UserStrategy s = map.get(user.getUserType);
s.doStuff();
}
@Autowired
public void setUserStrategies(List<UserStrategy> strategies) {
for (UserStrategy current : strategies) {
map.put(current.getUserType(), current);
}
}
}