Spring高级编程工厂介绍(一)

最近手头的项目刚验收完成,有了很多空余时间,第一次写blog有什么不对的地方请大家指正。


org.springframework.beans.factory.config.PropertyPathFactoryBean是一个很有用Bean工厂,大家在项目开发中用的比较多的依赖注入是对象实例注入,如果一个Bean构造函数中或方法中的参数不是一个对象而是普通的值或者是某个对象的一个属性,这时候就需要用到PropertyPathFactoryBean:

<bean class="com.test.Person" id="person"></bean> <property name="propertyPath"> <property name="targetBeanName">以下例子摘自wrox出版的《spring框架高级编程》中的一个例子,改了少量东西!
以后会继续更新!
</property> </property>

 

xml 代码
  1. <bean id="person" class="com.test.Person" />  
  2. <bean id="person.address.city" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">  
  3. <property name="targetObject">  
  4. <ref local="person" />  
  5. property>  
  6. <bean id="xxxBean" class="com.test.xxxBean">  
  7. <property name="cityName">  
  8. <ref local="person.address.city" />  
  9. property>  
  10. bean>  

 类com.test.Person中有个address类型的属性,而address中又有一个String类型的city属性,上面的配置是将city属性注入到
com.test.xxxBean的cityName属性中。

顺便提一下这里fckeditor好像有问题,编辑的内容有时候提交掉,代码编辑器会提交后会掉符号尤其是“>”,不知道其他人有
没有遇到过这种情况。

<bean class="org.springframework.beans.factory.config.PropertyPathFactoryBean&lt;/font&gt;" id="person.address.city"></bean>

 

 

org.springframework.beans.factory.config.FieldRetrievingFactoryBean能够获取类中的静态域变量,使用这个工厂在来注入常量很方便,代码如下:

xml 代码

 

  1. <bean id="max-long" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">  
  2. <property name="staticField">  
  3. <value>java.lang.Long.MAX_VALUEvalue>  
  4. property>  
  5. bean>  
  6.   

注入的时候只需要声明式的调用"max-long"来注入就行了。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值