Spring bean xml 配置继承


本人最近刚到一家新公司,在之前都使用spring 注解配置的方式来写代码的(个人比较喜欢注解的方式)。

但是由于接手的项目是用xml的方式,对于xml配置的方式比较不熟悉。

现在要整合hibernate试用两个数据源(A,B) , DAO层都继续一个父类(假如父类为BasicDaoImpl),该父类又包含  one,two,three属性。

xml注入bean是使用autowire="byType",根据类型注入。


	<bean id="one" class="com.xxx.One">
		<property name="dataSource" value="dataSource" />
	</bean>
	<bean id="two" class="com.xxx.Two">
		<property name="dataSource" value="dataSource" />
	</bean>
	<bean id="three" class="com.xxx.Three">
		<property name="dataSource" value="dataSource" />
	</bean>
	<bean id="oneDao" class="com.xxx.dao.OneDaoImpl" autowire="byType" />
	

但是 one,two,three都依赖数据源,所以必须明确指定是某个数据源.

	<bean id="dataSourceA" class="com.alibaba.druid.pool.DruidDataSource" />
	<bean id="dataSourceB" class="com.alibaba.druid.pool.DruidDataSource" />

	<bean id="oneA" class="com.xxx.OneA">
		<property name="dataSource" value="dataSourceA" />
	</bean>
	<bean id="twoA" class="com.xxx.TwoA">
		<property name="dataSource" value="dataSourceA" />
	</bean>
	<bean id="threeA" class="com.xxx.ThreeA">
		<property name="dataSource" value="dataSourceA" />
	</bean>


	<bean id="oneB" class="com.xxx.OneB">
		<property name="dataSource" value="dataSourceB" />
	</bean>
	<bean id="twoB" class="com.xxx.TwoB">
		<property name="dataSource" value="dataSourceB" />
	</bean>
	<bean id="threeB" class="com.xxx.ThreeB">
		<property name="dataSource" value="dataSourceB" />
	</bean>


	<bean id="oneDao" class="com.xxx.dao.OneDaoImpl">
		<property name="one" value="oneA" />
		<property name="two" value="twoA" />
		<property name="three" value="threeA" />
	</bean>
	<bean id="twoDao" class="com.xxx.dao.DaoImpl">
		<property name="one" value="oneB" />
		<property name="two" value="twoB" />
		<property name="three" value="threeB" />
	</bean>




但是 one,two,three都依赖数据源,所以必须明确指定是某个数据源

<property name="one" value="oneA" />
<property name="two" value="twoA" />
<property name="three" value="threeA" />

后面找到资料:http://www.yiibai.com/spring/spring-bean-configuration-inheritance.html,用抽象继承的方式,免去以上重复的配置

	<bean id="basicDaoImplA" class="com.xxx.BasicDaoImpl"
		abstract="true">
		<property name="one" value="oneA" />
		<property name="two" value="twoA" />
		<property name="three" value="threeA" />
	</bean>
	<bean id="basicDaoImplB" class="com.xxx.BasicDaoImpl" abstract="true">
		<property name="one" value="oneB" />
		<property name="two" value="twoB" />
		<property name="three" value="threeB" />
	</bean>
	<bean id="oneDao" class="com.xxx.dao.OneDaoImpl" parent="basicDaoImplA"/>
	<bean id="twoDao" class="com.xxx.dao.TwoDaoImpl" parent="basicDaoImplB"/>



抽象类需要配置abstract="true"。

还有其他类型的模版配置,可以点击查看

记录一下,第一次的写博客,请指教批评。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值