Java 会话作用域bean的行为类似于请求作用域bean
我有会话作用域bean,它的行为就像请求作用域bean 我在SingletonBean中使用它,也许这就是为什么第二个请求is有空值的原因? 我用来设置bean的代码: 在单例中:Java 会话作用域bean的行为类似于请求作用域bean,java,spring,session-scope,Java,Spring,Session Scope,我有会话作用域bean,它的行为就像请求作用域bean 我在SingletonBean中使用它,也许这就是为什么第二个请求is有空值的原因? 我用来设置bean的代码: 在单例中: @Autowired private MyBean myBean; @Service public class SingletonBean { @Autowired private MyBean myBean; // here I save the data to session scoped b
@Autowired
private MyBean myBean;
@Service
public class SingletonBean {
@Autowired
private MyBean myBean;
// here I save the data to session scoped bean
private void saveUser(LdapContext ctx, String principal) throws NamingException {
Attributes attributes = getAttributes();
myBean.setId("id");
myBean.setName("name");
myBean.setEmail("email");
myBean.setTelNum("123");
myBean.setGroups(Lists.newArrayList("one", "two"));
myBean.setAddress("address");
}
// here I try to read data from session scoped bean:
@Override
protected AuthorizationInfo queryForAuthorizationInfo(PrincipalCollection principals,
LdapContextFactory ldapContextFactory) throws NamingException {
// here userInfo will be null
List<String> userInfo = myBean.getGroups();
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
for (String role : userInfo ) {
authorizationInfo.addRole(role);
}
return authorizationInfo;
}
MyBean类:
@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)
public class MyBeanImpl implements MyBean, Serializable {
配置:
@Configuration
@ComponentScan(scopedProxy = ScopedProxyMode.INTERFACES, value = { "my.package.with.bean" })
public class ComponentsConfig {
}
MyBean是简单的pojo。有能手和二传手。
对于第一个请求,我在该bean上设置值;对于同一类singleton中的第二个请求,我希望读取这些值,但所有值都为null。不可能有什么东西超越了这些价值观
编辑
我是如何发出请求的——这只是简单的浏览器请求,而对会话bean进行读/写的代码则放在过滤器中
这是singleton:
@Autowired
private MyBean myBean;
@Service
public class SingletonBean {
@Autowired
private MyBean myBean;
// here I save the data to session scoped bean
private void saveUser(LdapContext ctx, String principal) throws NamingException {
Attributes attributes = getAttributes();
myBean.setId("id");
myBean.setName("name");
myBean.setEmail("email");
myBean.setTelNum("123");
myBean.setGroups(Lists.newArrayList("one", "two"));
myBean.setAddress("address");
}
// here I try to read data from session scoped bean:
@Override
protected AuthorizationInfo queryForAuthorizationInfo(PrincipalCollection principals,
LdapContextFactory ldapContextFactory) throws NamingException {
// here userInfo will be null
List<String> userInfo = myBean.getGroups();
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
for (String role : userInfo ) {
authorizationInfo.addRole(role);
}
return authorizationInfo;
}
}
当用户登录并通过身份验证时,我将他的详细信息保存在会话bean中。当他试图打开任何页面方法时,queryForAuthorizationInfo会在该对象中的筛选器链和值为空后执行。请向我们展示相关的读/写代码以及您如何发出请求。您能否发布您的Singleton实现,完成MyBeanImpl类和处理请求的类?我还将添加会话id,在保存到bean时和从会话bean读取时是相同的@IlyaOvesnov源代码added@SotiriosDelimanolis添加的源代码