Spring使用@scope("session")爆org.springframework.beans.factory.UnsatisfiedDependencyException

Spring Scope详解

起因

在做一个Web应用,想要把个人信息保存到session中,但是我用了记录信息的Bean的范围设置成 session IoC容器启动的时候就会爆出

org.springframework.beans.factory.UnsatisfiedDependencyException

网上找了一圈大多说法是
要在web.xml中配置这个listener

 <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

我配置了但是没有什么卵用。

解决

scope为session,request,globalSession的bean不能用@Autowired注入到默认为单例模式的bean中。


2016-09-30 update

再看《Spring in action》
3.4.1节:在设置scope的时候还要设置一个 proxyMode

//表示这个代理要实现该接口,并将调用委托给实现bean
proxyMode=ScopedProxyMode.INTERFACES 

//表示已生成目标类扩展的方式创建代理
proxyMode=ScopedProxyMode.TARGET_CLASS
posted @ 2016-09-14 15:15  A_yes  阅读(167)  评论(0编辑  收藏  举报