【问题标题】:share arraylsit among request in Spring controller在 Spring 控制器中的请求之间共享数组列表
【发布时间】:2017-06-04 10:17:53
【问题描述】:

我在 spring 控制器的 2 个 web 服务中使用了相同的 ArrayList,所以我想使用请求或会话范围从 DB 中获取一次我试图制作一个 bean

@WebAppConfiguration
public class exampleBean {
List<user> users = new ArrayList<user>();

@Bean
@Scope(value = "request")
public List<user> getuserList(){
    return users ;
}

@Bean
@Scope(value = "request")
public void setAttendessList(List<user>  userList){
    users = userList;
}

}

在控制器中

ApplicationContext ctx = new AnnotationConfigApplicationContext(exampleBean .class); AttendeesBean AttendeeBean = ctx.getBean(exampleBean .class);

在第一次服务中我设置了列表,但在第二次服务中没有找到它。

【问题讨论】:

  • 因为将范围设置为request,所以不要给它任何范围,那么它将被视为单例,并且可以通过您应用中的所有请求获得。
  • 另外,如果您不希望这个列表是静态的,那么您应该将scope 设为会话。
  • 我尝试将其更改为会话范围并且它有效@AmitK 谢谢
  • 您也可以通过单击我的答案前面的向上按钮来为答案投票,提前致谢。

标签: java spring spring-boot


【解决方案1】:

问题在于scope,当您定义request 范围时,值或bean 将在单个请求中可用,而对其他请求不可用。阅读spring scopes了解更多信息。

因此,一旦您将范围更改为session,那么在一个请求中创建的所有 bean/对象都将可用于同一会话中的所有其他请求。因此将范围更改为session 解决了您的问题。

【讨论】:

    【解决方案2】:

    我刚刚将 @Scope(value ="request") 更改为 @Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)

    使用@AmtiK 建议并进行一些搜索。

    成功了

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 1970-01-01
      • 1970-01-01
      • 2015-12-15
      • 2014-03-22
      • 1970-01-01
      • 2014-12-14
      • 2013-08-25
      • 2023-03-03
      相关资源
      最近更新 更多