使用AOP的Spring会话范围bean中的问题 [英] problem in Spring session scope bean with AOP

查看:83
本文介绍了使用AOP的Spring会话范围bean中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在HomeController类中注入currentUser实例。所以对于每个请求,HomeController都会有currentUser对象。

I want to inject currentUser instance in HomeController class. so for every request, HomeController will have currentUser object.

我的配置:

<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>

但我收到以下错误。

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/simple alternative?

干杯。

推荐答案

使用作用域代理,Spring在初始化上下文时仍需要知道bean的类型,在这种情况下,它无法这样做。你需要尝试给它更多的信息。

With scoped-proxies, Spring still needs to know the type of the bean when the context is initialized, and in this case it's failing to do so. You need to try and give it more information.

我注意到你只是指定了 factory-bean 您定义的 currentUser ,未指定 factory-method 。我实际上很惊讶这是一个有效的定义,因为这两者通常一起使用。因此,尝试将 factory-method 属性添加到 currentUser ,它指定 userProviderFactoryBean <上的方法/ code>创建用户bean。该方法需要具有 User 类的返回类型,Spring将使用该类来推断 currentUser 的类型。

I notice that you're only specifying factory-bean in your definition of currentUser, with no factory-method specified. I'm actually rather surprised that that's a valid definition, since the two are normally used together. So try adding the factory-method attribute to currentUser, which specifies the method on userProviderFactoryBean which creates the user bean. That method needs to have a return type of your User class, which Spring will use to infer the type of currentUser.

编辑:好的,在您的评论如下后,您似乎误解了如何在Spring中使用工厂bean。当你有一个类型为 FactoryBean 的bean时,你也不需要使用 factory-bean 属性。所以不是这样:

OK, after your comment below, it seems you've misunderstood how to use factory beans in Spring. When you have a bean of type FactoryBean, you don't need to use the factory-bean attribute as well. So instead of this:

<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>

您只需要:

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

此处, UserProvider FactoryBean ,Spring知道如何处理它。最终结果是 currentUser bean将是 UserProvider 生成的,而不是<$ c $的实例c> UserProvider 本身。

Here, UserProvider is a FactoryBean, and Spring knows how to handle that. The end result will be that the currentUser bean will be whatever UserProvider generates, rather than an instance of UserProvider itself.

出厂时使用 factory-bean 属性不是 FactoryBean 实现,而只是一个POJO,它允许您明确告诉Spring如何使用工厂。但是因为你使用 FactoryBean ,所以不需要这个属性。

The factory-bean attribute is used when the factory is not a FactoryBean implementation, but just a POJO, and it allows you to tell Spring explicitly how to use the factory. But because you're using FactoryBean, there's no need for this attribute.

这篇关于使用AOP的Spring会话范围bean中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆