新的 HashMap 实例变量不会在每个请求时都被创建?

huangapple go评论91阅读模式
英文:

New HashMap instance variable is not getting created for every request?

问题

我有一个SpringBoot应用程序,并在我的服务类中声明了一个HashMap实例变量 "myMap",如下所示:

public class MyService{
    Map<String,String> myMap = new HashMap<>();
    
    public void addStates(String stateName){
        myMap.put("state", stateName);
    }
    
    public void addCountries(String countryName){
        myMap.put("country", countryName);
    }
}

在我的第一个HTTP请求中,我调用了这个类,myMap 的值是:

[("state","Kerala"),("country","India")]


在第二个请求中,myMap 的值应该是:

[("state","Orissa"),("country","India")]

但是第二个请求中的实际myMap 值是:

[("state","Kerala"),("country","India")],
[("state","Orissa"),("country","India")]

似乎第一个创建的 myMap 变量也在后续请求中使用了,我希望每个请求都创建一个新的map变量。我不能将其添加为方法变量,因为它在类中的许多方法中都在使用。

请帮助解决这个问题。

英文:

I have a SpringBoot application and I have declared a HashMap instance variable "myMap" in my service class like this:

public class MyService{
Map&lt;String,String&gt; myMap = new HashMap&lt;&gt;();

public void addStates(String stateName){
myMap.put(&quot;state&quot;, stateName);
}

public void addCountries(String countryName){
myMap.put(&quot;country&quot;, countryName);
}
}

Say in my 1st HTTP request I have called this class and myMap values are

[("state","Kerala"),("country","India")]


2nd request myMap values should be

[("state","Orissa"),("country","India")]

But actual map values in 2nd request is

[("state","Kerala"),("country","India")],
[("state","Orissa"),("country","India")]

Seems like 1st myMap variable created is used in subsequent requests also, I want new map variable getting created for every request. I cannot add it as method variable as this is used in so many methods across the class.

Please help in resolving this.

答案1

得分: 0

默认情况下,Spring 中的 bean 作用域是 Singleton。因此,在您的情况下,MyService 类将被 Spring 视为单例。因此,通常只会创建一个您的服务类的对象,并且将在所有请求中使用相同的对象。
如果您希望在每个请求中创建一个服务类对象,在这种情况下,您可以使用 bean 的 Request 作用域。

@Service
@Scope("request")
public class MyService {
    Map<String, String> myMap = new HashMap<>();

    public void addStates(String stateName) {
        myMap.put("state", stateName);
    }

    public void addCountries(String countryName) {
        myMap.put("country", countryName);
    }
}
英文:

By default the scope of a bean in spring is Singleton. So in your case the MyService class will be treated by spring as singleton. So normally only one object of your service class will be created and same will be used for all the request.
If you want your service class object to be created for per request in such case you can sue the Request scope of the bean.

@Service
@Scope(&quot;request&quot;)
public class MyService{
Map&lt;String,String&gt; myMap = new HashMap&lt;&gt;();

public void addStates(String stateName){
myMap.put(&quot;state&quot;, stateName);
}

public void addCountries(String countryName){
myMap.put(&quot;country&quot;, countryName);
}
}

huangapple
  • 本文由 发表于 2020年7月29日 18:12:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63151282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定