Spring中depends-on的作用是什么?
来源:互联网 发布:孢子中文版for mac 编辑:程序博客网 时间:2024/05/18 03:05
http://yanln.iteye.com/blog/2210723
spring的IOC容器负责bean的管理,当实例化一个bean是,spring保证该Bean所依赖的其他bean已经初始化。一般情况下,用<ref>元素建立对其他bean的依赖关系。
比如:
- <bean id="a" class="com.yanln.spring.bean.A"> </bean>
- <bean id="b" class="com.yanln.spring.bean.B" p:a-ref="a"> </bean>
但在某些情况下,这种bean之间的依赖关系并不那么明显。
举个例子:某个论坛系统拥有很多系统参数(如会话过期时间、缓存更新时间等),这些系统参数用于控制系统的运行逻辑。比如:
- public class SystemSettings{
- //缓存更新时间
- public static int REFRESH_CYCLE = 60;
- ......
- }
在SystemSettings中为每一个系统参数提供了默认值,但对于一个灵活的论坛是需要提供一个管理后台的,在管理后台中可以调整这些系统参数并保存到后台数据库中,而在系统启动时,初始化程序从数据库后台加载这些系统参数的值来覆盖系统默认值。比如:
- public class SystemInit{
- public SystemInit(){
- //模拟从数据库中加载的系统参数配置值
- SystemSettings.REFRESH_CYCLE=100;
- ......
- }
- }
假设论坛系统有一个缓存刷新管理器,它需要根据系统参数SystemSettings.REFRESH_CYCLE创建缓存刷新定时任务:
- public class CacheManager{
- public CacheManager(){
- Timer t = new Timer();
- TimerTask cacheTask = new CacheTask();
- //缓存刷新定时处理
- t.schedule(cacheTask,0,SystemSettings.REFRESH_CYCLE);
- }
- ......
- }
在以上的实例中,CacheManager类需要用到SystemSetting类的值,而SystemSettings的值有SystemInit负责初始化,虽然CacheManager不直接依赖SystemInit,但从逻辑上看,CacheManager希望在SystemInit加载并完成系统参数设置后再启动,以获得最新的系统参数值。如果这三个bean都在spring配置文件中定义,我们如何保证SystemInit在CacheManager之前进行初始化呢?
spring允许用户通过depends-on属性指定bean前置依赖的bean,前置依赖的bean会在本bean实例化之前创建好:
- <bean id="sysinit" class="SystemInit">
- <bean id="manager" class="CacheManager" depends-on="sysinit"/>
- Spring中depends-on的作用是什么?
- Spring中depends-on的作用是什么?
- Spring 中 bean 之间的关系:parent(继承) 和 depends-on (依赖)作用是什么?
- Spring的 depends-on
- spring的depends-on属性
- spring的懒加载和depends-on
- Spring的depends-on属性Bean依赖
- Spring的depends-on属性Bean依赖
- Spring depends-on
- Spring depends-on
- Spring depends-on介绍
- spring depends-on
- 【spring bean】spring中bean的懒加载和depends-on属性设置
- depends-on的用法
- Spring 标签属性 depends-on
- Spring管理Bean之间的关系一(depends-on)
- <bean>定义中元素ref 和depends-on 的区别
- Spring bean加载顺序 depends-on
- JavaScript 实现base64数据加密
- android颜色表
- Python2.7获取QQ照片墙
- Android Studio logcat 设置 缓存大小 ,增加logcat条数
- 用心做点事,写点代码的哲学(编程的基本功)
- Spring中depends-on的作用是什么?
- csdn markdown 的使用
- 支持向量机(三)核函数
- spring配置packagesToScan的问题
- 使用 Notification 在状态栏中显示一个通知。
- 支持向量机SVM(四)
- 数据库存取图片
- Access denied for user ''@'localhost' to database 'xxx‘
- [Built-in Functions] - P