java - Spring框架,我需要一个变量来依赖于 session

标签 java spring spring-mvc

我正在制作一个简单的文件服务器应用程序。该应用程序正在扫描文件的当前位置并将其显示给用户。

我需要一个变量,当前路径是 session 本地的。我想避免一个用户将目录更改为另一用户时的情况。

原样:

User1 is opening home page (current location Z:/), user1 is changing directory to Z:/foo, User2 is opening home page and he is home page is Z:/foo.

成为:

User1 is opening home page (current location Z:/), user1 is changing directory to Z:/foo, User2 is opening home page and he is home page is Z:/, but User1 is still at Z:/foo

我试图使用@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)

但是要么我(在编码中)犯了一些失败,要么它对我不起作用

最佳答案

您必须创建单独的 session 范围 bean,并在需要 session 范围数据时注入(inject)它 - 即使在应用程序范围 bean 中也是如此。例如

@Scope(value = WebApplicationContext.SCOPE_SESSION, proxyMode = ScopedProxyMode.TARGET_CLASS)
@Component
public class UserSpace{

    private File userRoot=....somehow set that on bean creation - eg at @PostConstruct
   .....other fields if needed
}

然后

@Service
class YoutService{

  @Autowired
  private UserSpace userSpace;// this will be session scoped proxy

}

关于java - Spring框架,我需要一个变量来依赖于 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58273953/

相关文章:

java - 我在使用 ajax 将 json 数据发送到 Controller 时遇到问题

java - 部署 Spring 4.0.1 应用程序失败

jakarta-ee - Web 项目中的 ClassNotFoundException

java - Hadoop,运行任务

java - 我的 eclipse 打不开..为什么?

java - 使用随机数生成 X/0 模式(无硬编码)

Java - 指定要在 REST 中返回的字段

spring - Spring MVC 中的简单搜索

java - Tomcat 无法加载我的 jsp 文件 - 服务器未找到当前表示

Java spring Autowiring Jdbctemplate 但不 Autowiring NamedParameterJdbcTemplate