0
私のコードでState Patternを使用しようとしました。しかし、これがSpringとの競合状態につながるかどうかはわかりません。ここでactive
stop
restart
は異なる実装を持っています。どちらの実装が実行されるかは、setCurrentInt
を呼び出すことによって何が設定されたかによって異なります。はい、この問題に対処する方法はありますか。注入されていないフィールドがSpringの競合状態になることはありますか?
@Component
public class StateService {
//3 states
@Autowired
@Qualifier("notActivatedState")
private ActiveState notActivatedState;
@Autowired
@Qualifier("stoppedState")
private ActiveState stoppedState;
@Autowired
@Qualifier("inUseState")
private ActiveState inUseState;
//current state
private Integer currentInt;
//Interface which was delegated to perform an act. It is not @Autowired, could there be some problem when multiple requests set currentInt to different values ?
private ActiveState currentState;
public void activate(BdCorp bdCorp) {
currentState.activate(bdCorp);
}
public void stop(BdCorp bdCorp) {
currentState.stop(bdCorp);
}
public void restart(BdCorp bdCorp) {
currentState.restart(bdCorp);
}
public void setCurrentInt(Integer currentInt) {
this.currentInt = currentInt;
if (currentInt == 1) {
this.currentState = notActivatedState;
}
if (currentInt == 2) {
this.currentState = inUseState;
}
if (currentInt == 3) {
this.currentState = stoppedState;
}
}
}
感謝を使用!また、@Scope( "prototype")の上位StateServiceクラスを使用して解決策を見つけました。したがって、すべての要求が異なる 'stateService'インスタンスによって処理されました。これは並行性の問題も防ぐことができますか?@StanislavL – fuko