2016-04-13 69 views
0

我有一个简单的POJO它UserQuota与1场quota为什么我的Spring会话范围bean在会话中共享?

@Component 
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) 
public interface UserQuota { 
    public int getQuota(); 
    public void setQuota(int quota); 
} 

现在,我用了两个不同的浏览器窗口(Firefox和Chrome)登录到我的web应用程序作为两个不同的用户。令我惊讶的是,当我从一个会话中设置配额值(使用setQuota)时,新值可用于其他会话(当调用getQuota时)。我期待每个用户会话都有自己的bean实例;是不是春季会话scoped bean是什么?

我必须缺少一些东西。会是什么呢?

编辑:

实现类看起来是这样的:

@Component 
public class UserQuotaImpl implements UserQuota { 

    private int quota; 

    /** 
    * @return the quota 
    */ 
    public int getQuota() { 
     return quota; 
    } 

    /** 
    * @param quota the quota to set 
    */ 
    public void setQuota(int quota) { 
     this.quota = quota; 
    } 

} 

,并终于在这里是如何使用的会话bean:

@Component 
public class UserQuotaHandler { 

    @Autowired 
    private UserQuota userQuota; 

    public void checkAndUpdateQuota() { 
     int quota = userQuota.getQuota(); 

     // i use my business logic to decide whether the quota needs an update 
     if(myBusinessLogic) { 
      userQuota.setQuota(someNewValue); 
     } 
    } 

} 

我使用context:component-scan在我的XML配置文件。可以注意到,我的大多数其他自动装配Autowired豆是单身豆类这似乎预期

+0

请说明如何注入和使用bean。 – Savior

+0

我已根据要求更新了我的问题 – Tanvir

+0

实施课在哪里?你会想用'@ Scope'注解,而不是界面afaik。 – Savior

回答

2

你会想在你的情况下,会议@ScopeUserQuotaImpl来注释具体bean类一直在努力。

Spring在您的具体类的任何超类或超接口上忽略@Scope。而且,由于你的类型没有任何明确的@Scope注释

@Component 
public class UserQuotaImpl implements UserQuota { 

春假设你的意思是让一个singleton bean。

+0

,这意味着我的请求范围的bean也有同样的问题!只是会话scoped bean的问题首先出现在表面 – Tanvir

+1

@Tanvir通常,不要认为注释是“继承的”。 – Savior