我正在制作一个简单的文件服务器应用程序。该应用程序正在扫描文件的当前位置并将其显示给用户。
我需要一个变量,当前路径是 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/