2017-06-14 53 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