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


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

以下例子摘自wrox出版的《spring框架高级编程》中的一个例子,改了少量东西!
以后会继续更新!

 

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好像有问题,编辑的内容有时候提交掉,代码编辑器会提交后会掉符号尤其是“>”,不知道其他人有
没有遇到过这种情况。

 

 

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"来注入就行了。

 

评论
发表评论

您还没有登录,请登录后发表评论