I've developed a Spring Web-MVC application. I have some offices in my project. Each user belongs to an office. user.getOfficeType()
returns an integer representing the user's office type. If the office type is 1, the user belongs to Office1 and etc.
However I want to inject the authenticated user's office into my service classes:
class MyService{
@Autowired
Office currentOffice;
...
}
I read the Spring docs. I need a session scoped bean to inject it into my service classes.
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>
I have three implementations of the Office
interface. Once a user requests a resource, I want to be aware of his Office. So I need to inject his session-scoped Office into my service classes. But I don't know how to instantiate it according to the user's office. please help!