我有一个简单的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豆是单身豆类这似乎预期
请说明如何注入和使用bean。 – Savior
我已根据要求更新了我的问题 – Tanvir
实施课在哪里?你会想用'@ Scope'注解,而不是界面afaik。 – Savior