如何根据用户属性创建 Spring 会话范围的 bean

How to create a Spring session scoped bean based on user properties

我开发了一个 Spring Web-MVC 应用程序。我的项目中有一些办公室。每个用户都属于一个办公室。 user.getOfficeType() returns 代表用户办公室类型的整数。如果办公类型为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 注入到我的服务中 类。但是我不知道如何根据用户的办公室来实例化它。请帮忙!

我找到了解决办法!我声明了一个 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);
   }


}

我只是抛出一个不同的方法

@Configuration
public class SessionScopeBeansConfig {

    @Bean
    @SessionScope(proxyMode = ScopedProxyMode.TARGET_CLASS)
    public User user( UserDAO userDAO ) {

        return userDAO.getUserByLoginName( SecurityContextHolder.getContext().getAuthentication().getName() );
    }


    @Bean
    @SessionScope(proxyMode = ScopedProxyMode.TARGET_CLASS)
    public Office office( User user ) {

        Office office = <<Code to initialize office>>;

        return office;
    }
}

这样就可以直接注入 Office

class MyService{
   @Autowired
   Office currentOffice;
   ...
}