可以手工注入bean,不过它的生命周期就要靠自己的代码控制了。sfb=new LocalSessionFactoryBean();
sfb.setDataSource(dataSource);
sfb.afterPropertiesSet();mySessionFactory=(SessionFactory)sfb.getObject();
sfb.setDataSource(dataSource);
sfb.afterPropertiesSet();mySessionFactory=(SessionFactory)sfb.getObject();
</bean>
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref local="mydataSource"/>
</property>
</bean>
直接在bean内定义private ComboPooledDataSource mydataSource;
然后对mydataSource进行getter 和setter应该就可以了啊
sfb=new LocalSessionFactoryBean();
sfb.setDataSource(dataSource);
sfb.afterPropertiesSet();mySessionFactory=(SessionFactory)sfb.getObject();你的这个方法我如何在 sping 的 AbstractApplicationContext中得到?
to:zghmu007()
这也是我目前只能使用的方法了
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
ComboPooledDataSource cp = new ComboPooledDataSource;
..........
beanFactory.getBeanDefinition("mySessionFactory").getPropertyValues().addPropertyValue("dataSource", cp );
}
});context.refresh();重载整个applicationContext是必然的,为什么有这样的需求?
1.现在的sping,webwork这些东西用起来是好用,就是配置文件太多了。我想尽量的简化配置。
2.部分配置我想动态载入,有意义才载入,不必要的不载入.
3.载入的bean不同,软件的功能可能不同.
4.sping 实现插件功能,扫描有那些插件,然后对应的载入sping.利用sping 的容器可以实现很多不错的功能。能够动态载入,就可以自由的控制。
读取配置文件,然后写入这样做就不入,直接生成配置文件,在导入了。
ComboPooledDataSource dataSource = new ComboPooledDataSource
sfb=new LocalSessionFactoryBean();
sfb.setDataSource(dataSource);
sfb.afterPropertiesSet();
mySessionFactory=(SessionFactory)sfb.getObject();如上,在某处初始化SessionFactory,然后用静态变量保持,在dao里面
new HibernateTemplate(getSessionFactory()).execute()......
如果要用Spring进行手动事务控制话,Spring文档里也有详细的例子。
非常感谢
看了一下 PicoContainer 感觉还真不错。不知道为什么sping 还没有支持动态注入,可能是我还不会吧 :)
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
ComboPooledDataSource cp = new ComboPooledDataSource();//(ComboPooledDataSource)beanFactory.getBean("mydb");
beanFactory.registerSingleton("mydb",cp);
if (beanFactory.containsBean("mydb"))
{ System.out.println("-------------------------cp1=" + cp);
ComboPooledDataSource cp2 = (ComboPooledDataSource)beanFactory.getBean("mydb");
System.out.println("-------------------------cp2=" + cp2);
} else System.out.println("---------------------没有注册成功");