Problem in Spring scope bean session with AOP

I want to add an instance of currentUser to the HomeController class. therefore, for each request, HomeController will have a currentUser object.

My configuration:

<bean id="homeController" class="com.xxxxx.actions.HomeController"> <property name="serviceExecutor" ref="serviceExecutorApi"/> <property name="currentUser" ref="currentUser"/> </bean> <bean id="userProviderFactoryBean" class="com.xxxxx.UserProvider"> <property name="userDao" ref="userDao"/> </bean> <bean id="currentUser" factory-bean="userProviderFactoryBean" scope="session"> <aop:scoped-proxy/> </bean> 

But I get the following error.

 Caused by: java.lang.IllegalStateException: Cannot create scoped proxy for bean 'scopedTarget.currentUser': Target type could not be determined at the time of proxy creation. at org.springframework.aop.scope.ScopedProxyFactoryBean.setBeanFactory(ScopedProxyFactoryBean.java:94) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1350) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540) 

What is the problem? and is there any better / simpler alternative?

Greetings.

+3
source share
1 answer

With scoped-proxies, Spring still needs to know the type of bean when the context is initialized, in which case it cannot do this. You need to try and provide him with more information.

I noticed that you are specifying factory-bean in your currentUser definition, without specifying factory-method . In fact, I'm pretty surprised that this is the correct definition, since the two are commonly used together. So try adding the factory-method attribute to currentUser , which points to the userProviderFactoryBean that creates the user bean. This method must have a return type of your User class, which Spring will use to output the currentUser type.


Edit: Well, after your comment below, it looks like you misunderstood how to use factory beans in Spring. When you have a bean of type FactoryBean , you do not need to use the factory-bean attribute. So instead:

 <bean id="userProviderFactoryBean" class="com.xxxxx.UserProvider"> <property name="userDao" ref="userDao"/> </bean> <bean id="currentUser" factory-bean="userProviderFactoryBean" scope="session"> <aop:scoped-proxy/> </bean> 

You just need to:

 <bean id="currentUser" class="com.xxxxx.UserProvider" scope="session"> <aop:scoped-proxy/> <property name="userDao" ref="userDao"/> </bean> 

Here UserProvider is a FactoryBean , and Spring knows how to handle this. The end result is that the currentUser bean will be what the UserProvider generates, not the UserProvider instance.

The factory-bean attribute is used when the factory is not an implementation of FactoryBean , but just a POJO, and allows you to explicitly tell Spring how to use the factory. But since you are using FactoryBean , there is no need for this attribute.

+4
source

Source: https://habr.com/ru/post/1341192/


All Articles