springautowiredspring-beanxml-configurationrequestscope

Spring RequestScope of method in XML-config


I have an module with the following:

@Configuration
public class UserHolder {

    @Bean
    @RequestScope
    public IUser getUser() {
         return (IUser)SecurityContextHolder.getContext().getAuthentication();
    }
}

IUser is an Interface and this works fine for all java-config modules that use this bean. @Autowired IUser user

However, we have an older project with xml-config. There, the bean is initialized on deploy time (which is of course null), but there is no proxy to get the correct user at request time.

What I've written now (which is obviously incorrect):

<bean class="...UserHolder" scope="request">
    <aop:scoped-proxy/>
</bean>

I would need to define the bean in UserHolder: getUser in the XML. But this returns an interface, so I can't define the class there...

<bean class="...UserHolder"></bean>
<bean class"...IUser" scope="request"> <!-- Interface can't be inserted here -->
     <aop:scoped-proxy/>
</bean>

Spring versions for the module that defines UserHolder are:

Spring versions for the module with xml-config that uses the UserHolder module are:

Any help would be appreciated!

Thx!


Solution

  • I managed to fix it with factory-beans:

    <bean id="userHolder"
          class="...UserHolder">
    </bean>
    
    <bean id="user"
          class="...IUser"
          factory-bean="userHolder"
          factory-method="getUser"
          scope="request"
          primary="true">
        <aop:scoped-proxy/>
    </bean>