java – 如何基于用户属性创建Spring会话范围的bean
作者:互联网
我开发了一个Spring Web-MVC应用程序.我的项目中有一些办公室.每个用户都属于一个办公室. user.getOfficeType()返回表示用户办公室类型的整数.如果办公室类型为1,则用户属于Office1等.
但是,我想将经过身份验证的用户办公室注入我的服务类:
class MyService{
@Autowired
Office currentOffice;
...
}
我读了Spring文档.我需要一个会话范围的bean来将它注入我的服务类.
applicationContext.xml中:
<mvc:annotation-driven />
<tx:annotation-driven transaction-manager="hibernateTransactionManager"/>
<context:annotation-config />
<context:component-scan base-package="com.package.controller" />
<context:component-scan base-package="com.package.service" />
...
<bean id="office" class="com.package.beans.Office" scope="session">
<aop:scoped-proxy/>
</bean>
我有三个Office界面的实现.一旦用户请求资源,我想知道他的Office.所以我需要将他的会话范围的Office注入我的服务类.但我不知道如何根据用户的办公室实例化它.请帮忙!
解决方法:
我找到了解决方案!我声明了一个OfficeContext,它包装Office并实现它.
@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class OfficeContext implements InitializingBean, Office {
private Office office;
@Autowired
private UserDao userDao;
@Autowired
private NoneOffice noneOffice;
@Autowired
private AllOffice allOffice;
@Autowired
private TariffOffice tariffOffice;
@Autowired
private ArzeshOffice arzeshOffice;
public Office getOffice() {
return this.office;
}
@Override
public void afterPropertiesSet() throws Exception {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth.isAuthenticated()) {
String name = auth.getName(); //get logged in username
JUser user = userDao.findByUsername(name);
if (user != null) {
this.office = noneOffice;
} else {
OfficeType type = user.getOfficeType();
switch (type) {
case ALL:
this.office = allOffice;
break;
case TARIFF:
this.office = tariffOffice;
break;
case ARZESH:
this.office = arzeshOffice;
break;
default:
this.office = noneOffice;
}
}
} else {
this.office = noneOffice;
}
}
@Override
public OfficeType getType() {
return office.getType();
}
@Override
public String getDisplayName() {
return office.getDisplayName();
}
}
在我的服务类中,我注入了OfficeContext.
@Service
public class UserService {
@Autowired
UserDao userDao;
@Autowired
OfficeContext office;
public void persist(JUser user) {
userDao.persist(user);
}
public void save(JUser user) {
userDao.save(user);
}
}
标签:java,spring-mvc,spring,spring-bean,session-scope 来源: https://codeday.me/bug/20190628/1314609.html