如何同时在@Component类中使用字段?

时间:2017-05-10 13:21:08

标签: java spring

遵循以下代码:

@Component
public class ClassC extends ClassA<T> implements ClassB {

    private String preferredDateTimeFormat = null;

我的进程在调用来自客户端的请求后使用此类。

根据每个请求,我想保留preferredDateTimeFormat值,实际上是什么,preferredDateTimeFormat变量用于我的所有请求。

我能做些什么来解决它?

3 个答案:

答案 0 :(得分:1)

在您的课程中使用@Scope,其值为request

像这样

@Component
@Scope(value="request", proxyMode =ScopedProxyMode.TARGET_CLASS)
public class ClassC extends ClassA<T> implements ClassB {

  private String preferredDateTimeFormat = null;

您可以在Spring Bean Scope here

中找到更多信息

答案 1 :(得分:0)

有多种方式。

您可以定义一个Request Scope bean并将preferredDateTimeFormat放在那里。

OR

您可以定义ThreadLocal属性并将值放在那里。

答案 2 :(得分:0)

我不完全确定您是否想要每次请求一个ClassC实例或每次使用新实例但您可能希望查看@Scope注释及其值PrototypeRequest

请参阅Spring Bean Scopes