最近手头的项目刚验收完成,有了很多空余时间,第一次写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>
- <bean id="person" class="com.test.Person" />
- <bean id="person.address.city" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
- <property name="targetObject">
- <ref local="person" />
- property>
- <bean id="xxxBean" class="com.test.xxxBean">
- <property name="cityName">
- <ref local="person.address.city" />
- property>
- bean>
类com.test.Person中有个address类型的属性,而address中又有一个String类型的city属性,上面的配置是将city属性注入到
com.test.xxxBean的cityName属性中。
顺便提一下这里fckeditor好像有问题,编辑的内容有时候提交掉,代码编辑器会提交后会掉符号尤其是“>”,不知道其他人有
没有遇到过这种情况。
<bean class="org.springframework.beans.factory.config.PropertyPathFactoryBean</font>" id="person.address.city"></bean>
org.springframework.beans.factory.config.FieldRetrievingFactoryBean能够获取类中的静态域变量,使用这个工厂在来注入常量很方便,代码如下:
- <bean id="max-long" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
- <property name="staticField">
- <value>java.lang.Long.MAX_VALUEvalue>
- property>
- bean>
注入的时候只需要声明式的调用"max-long"来注入就行了。