Spring 有生命周期的bean的依赖注入
来源:互联网 发布:淘宝店宝宝怎么样 编辑:程序博客网 时间:2024/05/20 18:04
Spring 有生命周期的bean的依赖注入
Spring IoC容器不仅为你初始化对象,并且会为你关联那些互相协作的bean或者依赖,如果你想将一个生命周期为HTTP request的bean注入到另一bean中,你必须在这个bean中注入一个AOP代理,这个代理具有和这个bean相同的接口,当调用这些API的时候,实际会委托调用真实的对象。至于为什么要这么做,可以看下下面的例子。
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
- <!-- an HTTP Session-scoped bean exposed as a proxy -->
- <bean id="userPreferences" class="com.foo.UserPreferences" scope="session">
- <!-- instructs the container to proxy the surrounding bean -->
- <aop:scoped-proxy/>
- </bean>
- <!-- a singleton-scoped bean injected with a proxy to the above bean -->
- <bean id="userService" class="com.foo.SimpleUserService">
- <!-- a reference to the proxied userPreferences bean -->
- <property name="userPreferences" ref="userPreferences"/>
- </bean>
- </beans>
为了创建这个代理,你在bean中插入了这么一个元素<aop:scoped-proxy/>,(如果你选择使用基于类的代理,你需要在你的CLASSPATH里加入CGLIB jar包,默认选择CGLIB),为什么除了singleton,和prototype生命周期的bean都需要<aop:scoped-proxy/>呢?那让我们回过头来看看prototype和前面提到的session的scope(注意:以下这段配置是不完整的,只作说明用)。
- <bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>
- <bean id="userManager" class="com.foo.UserManager">
- <property name="userPreferences" ref="userPreferences"/>
- </bean>
这个例子中,userManager是一个scope为singleton的bean,并且为他注入一个Http session周期的bean: userPreferences,很明显,userManager在容器中是一个单例,他只会在容器中被实例化一次,他的依赖也只会被注入一次,也就是说实际上userManager每次都操作的都是相同的userPreferences对象。这样的话即违背你之前想要的结果,把一个短生命周期的bean注入到一个具有长生命周期的bean当中,但是为了考虑HttpSession的生命周期,所以你必须让userPreferences指明他的生命周期,因此容器创建一个和userPreferences具有相同接口的代理对象,他会去请求真正的userPreferences操作,容器将这个代理注入给userManager,但这一切对userManager却是透明的,当他调用userPreferences时,实际是在调用这个代理,代理再去取得实际的HttpSession中得userPreferences,委托调用userPreferences的方法。
因此,当你要注入的bean的scope是request,session或globalSession时,正确完整的写法应该是下面这样的:
- <bean id="userPreferences" class="com.foo.UserPreferences" scope="session">
- <aop:scoped-proxy/>
- </bean>
- <bean id="userManager" class="com.foo.UserManager">
- <property name="userPreferences" ref="userPreferences"/>
- </bean>
到这就说完了,实际上以上内容是一个理解Proxy设计模式的极好例子!
- Spring 有生命周期的bean的依赖注入
- Spring 有生命周期的bean的依赖注入
- Spring的配置、生命周期、依赖注入bean的操作
- Spring中bean的依赖注入解析
- spring源码解读 Bean的依赖注入
- spring 依赖注入bean的属性
- 11级_Java_曹建波 04.24 Spring的配置、生命周期、依赖注入bean的操作
- 【spring-bean】浅谈Spring的依赖注入(基础)
- spring的管理bean和依赖注入的原理剖析
- spring 配置bean的方法及依赖注入发方式
- Spring-2:bean的两种依赖注入方式
- (学习笔记)spring入门之Bean的依赖注入
- Spring Bean的生命周期
- Spring Bean的生命周期
- spring - bean的生命周期
- Spring Bean的生命周期
- spring bean 的生命周期
- Spring bean的生命周期
- java实现webservice实例
- Linux学习笔记——初级系统管理命令
- ARMlinux kernel 启动之start_kernel (二)
- Eclipse 快捷键及设置
- HDU1859:最小长方形
- Spring 有生命周期的bean的依赖注入
- char*,const char*和string的区别以及相互转换
- 如何发表高水平论文(转载,非常好)
- linux下的RPC
- unity3d Quaternion 计算3D物体旋转角度
- Ubuntun下配置android开发环境及遇到的相关问题
- Hadoop源码分析之心跳机制
- CSS overflow:hidden的理解
- C#多线程操作界面控件