简体   繁体   English

字符串在之前的 state 中,即使在触发新请求后也是如此 java

[英]string being in previous state even after triggering a new request java

I am working on a spring boot api where I have used a query string and doing some replace operations on it on every get request but even after I trigger a new request the string is in the same state as that of previous GET call and the query gets messed up.我正在处理一个 spring 启动 api,我在其中使用了一个查询字符串并在每个 get 请求上对其进行了一些替换操作,但即使在我触发了一个新请求之后,该字符串与之前的GET调用和查询的字符串在相同的 state 中搞砸了。

Here's my code:这是我的代码:

 private static String GET_SETS = "Select * from table #check#";

Now I have 1 method in the same repository which gets called on a get request:现在我在同一个存储库中有 1 个方法,它在获取请求时被调用:

public PagedList getSets(Map params) {
    if (!StringCheck.isEmpty(flattenMap.get("entity_name"))) {
        GET_SETS = GET_CODE_SETS.replace("#check#", " WHERE e.#entity_name# = ?");
        values.add((String)flattenMap.get("entity_name"));
    } else {
        GET_SETS = GET_SETS.replace("#check# #status_check#", "");
    }
}

Now whenever a GET request is triggered,some changes are done to GET_SETS string according to map values,and again in the next requests the same changes are there.现在每当触发GET请求时,都会根据 map 值对GET_SETS字符串进行一些更改,并且在下一个请求中再次进行相同的更改。

How to solve this?如何解决这个问题? I want the query string to be whats defined at the start on every request.我希望查询字符串是在每个请求开始时定义的内容。 Thanks谢谢

Your problem is most likely caused by using the default scope, which is singleton, when creating your Spring components.您的问题很可能是由于在创建 Spring 组件时使用默认的 scope 引起的,即 singleton。

Spring provides the following scopes for creating beans ( source ): Spring 提供了以下用于创建 bean 的范围( 来源):

  • singleton (default): Scopes a single bean definition to a single object instance for each Spring IoC container singleton(默认):将单个 bean 定义的范围限定为每个 Spring IoC 容器的单个 object 实例
  • prototype: Scopes a single bean definition to any number of object instances.原型:将单个 bean 定义的范围限定为任意数量的 object 个实例。
  • request: Scopes a single bean definition to the lifecycle of a single HTTP request. request:将单个 bean 定义的范围限定为单个 HTTP 请求的生命周期。 That is, each HTTP request has its own instance of a bean created off the back of a single bean definition.也就是说,每个 HTTP 请求都有自己的 bean 实例,该实例是在单个 bean 定义的基础上创建的。 Only valid in the context of a web-aware Spring ApplicationContext.仅在 web-aware Spring ApplicationContext 的上下文中有效。
  • session: Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext. session:将单个 bean 定义的范围限定为 HTTP Session 的生命周期。仅在 web-aware Spring ApplicationContext 的上下文中有效。
  • application: Scopes a single bean definition to the lifecycle of a ServletContext. application:将单个 bean 定义的范围限定为 ServletContext 的生命周期。 Only valid in the context of a web-aware Spring ApplicationContext.仅在 web-aware Spring ApplicationContext 的上下文中有效。
  • websocket: Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext. websocket:将单个 bean 定义的范围限定为 WebSocket 的生命周期。仅在 web-aware Spring ApplicationContext 的上下文中有效。

If we don't specify any scope explicitly, the default singleton scope is used.如果我们没有明确指定任何 scope,则使用默认值 singleton scope。 This means that the service/component is shared between multiple injections, hence it is reused for multiple GET requests.这意味着服务/组件在多个注入之间共享,因此它被多个GET请求重用。

In order to get rid of this behavior, we might use something like request scope, although in this case we have to take care of thread-safety as well.为了摆脱这种行为,我们可能会使用请求 scope 之类的东西,尽管在这种情况下我们也必须注意线程安全。

Other solution is to not use member variables and try to use local variables for things such as GET_SETS .其他解决方案是不使用成员变量并尝试对诸如GET_SETS类的东西使用局部变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM