2017-06-14 55 views
0

我有單身範圍如下豆:修改與單範圍bean的屬性在Spring

public class MyImpl implements MyInterface { 

    private HashMap<String, String> config = new HashMap<>(); 


    private void load(String check) { 
     if ("abc".equalsIgnoreCase(check)) { 
      config.put("key", "val"); 
     } 
     else { 
      config.put("key", "val_else"); 
     } 
    } 

    @Override 
    public HashMap<String, String> getConfig(String check) { 
     load(check); 
     return config; 
    } 
} 

在其它類的,我注入MyImpl,並嘗試使用如下配置:

@Service 
    public class Service { 
    @Inject 
    MyInterface impl; 
    public doJob(String check){ 
     HashMap<String, String> config = impl.getConfig(check); 
     String myValue= config.get("key"); 
     //some other code 
    } 
    } 

如果我有請求/秒的100S,如果檢查的價值是有一定的要求ABC和和別的其他請求,我還會就吃g不同的值myValue?我試圖推廣代碼,因爲我無法在此分享確切的代碼。我的問題是我們可以修改單個bean的每個請求的屬性嗎?

回答

0

創建一個ThreadLocal存儲(請參閱the example)以避免該問題。

或者,您可以將豆的範圍更改爲REQUEST