Spring中依赖配置的深入理解

依赖关系的深入理解
1.注入其他Bean的属性值
PropertyPathFactoryBean用来获得目标Bean的属性值
public class Person1 {
private int age;
private Son son;
    省略了set方法
}
public class Son {
private int age;
省略了set方法,添加一个toString()方法
}
<!-- 定义了将要被引用的目标Bean -->
    <bean id="person1" class="cn.spring.study04.Person1">
        <property name="age" value="30"></property>
        <property name="son">
            <!-- 使用嵌套bean -->
            <bean class="cn.spring.study04.Son">
               <property name="age" value="11"></property>
            </bean>
        </property>
    </bean>
    <!-- son1的age属性依赖于person1的属性值 -->
    <bean id="son1" class="cn.spring.study04.Son">
        <property name="age">
            <bean id="person1.son.age" class="org.springframework.beans.factory.config.PropertyPathFactoryBean"></bean>
        </property>
    </bean>
也可以采用下列配置
<bean id="son1" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
         <!-- 确定目标bean,表明son1 Bean来自哪个Bean的属性 -->
         <property name="targetBeanName" value="person1"></property>
         <!-- 确定属性表达式,表明son1 Bean来自目标Bean的哪个属性 -->
         <property name="propertyPath" value="son"></property>
</bean>
其中,targetBeanName或targetObject用于指定目标Bean,确定获取哪个Bean的属性值
propertyPath用于指定属性,确定要获取目标Bean的哪个属性值.上述想要获取son属性的age属性,也可以采用son.age
也可以使用嵌套Bean实例
<bean id="son2" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
        <!-- 确定目标Bean,这里采用了嵌套Bean定义目标Bean -->
        <property name="targetObject">
            <bean class="cn.spring.study04.Person1">
                <property name="age" value="30"></property>
            </bean>
        </property>
        <!-- 确定属性表达式,表明son1 Bean来自目标Bean的哪个属性 -->
        <property name="propertyPath" value="age"></property>
</bean>
2.注入其他Bean的Field值
通过FieldRetrievingFactoryBean类可以将其他Bean的Field值注入给其他Bean
首先定义一个static的属性
public class SonField {
   public static int NUM = 30;  
}
<!-- 强SonField的NUM值付给son的age属性 -->
    <bean id="son3" class="cn.spring.study04.Son">
        <property name="age">
            <bean id="cn.spring.study04.SonField.NUM" 
              class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"></bean>
        </property>
    </bean>
Field既可以是静态的,也可以是非静态的。如果是非静态的,则不能通过类名直接访问Field,可以如下配置
<!-- 将Field值定义成Bean实例 -->
<bean id="age1" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
        <!-- targetClass指定Field所在的目标类 -->
        <property name="targetClass" value="cn.spring.study04.SonField"></property>
        <!--targetField指定Field名  -->
        <property name="targetField" value="NUM"></property>
</bean>
->targetClass或targetObject:指定Field值所在的目标类或目标对象,如果Field是静态的,则使用targetClass;否则使用targetObject
->targetField用于指定目标Field的Field名
还有一种更简洁的写法
<bean id="age1" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
        <property name="staticField" value="cn.spring.study04.SonField.NUM"></property>
</bean>
3.注入其他Bean的方法返回值
通过MethodInvokingFactoryBean工厂Bean,可将指定方法返回值注入成目标Bean的属性值
首先定义一个返回值得类
public class ValueGenerator {
public int getValue(){
return 2;
}
public static int getStaticValue(){
return 9;
}
}
然后去配置
<!-- 定义成目标Bean,后面将会获取该Bean的方法返回值 -->
<bean id="valueGenerator" class="cn.yxt.study01.ValueGenerator"></bean>
<!-- 定义名为son1的Bean -->
<bean id="son1" class="cn.yxt.study01.Son">
    <property name="age">
        <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <!-- targetObject确定目标Bean,指定调用哪个Bean -->
        <property name="targetObject" ref="valueGenerator"></property>
        <!-- targetMethod确定目标方法,指定调用目标Bean的哪个方法 -->
        <property name="targetMethod" value="getValue"></property>
        </bean>
    </property>
</bean>
->targetObject确定目标Bean,该Bean可以是容器中已有的Bean,也可以是嵌套Bean.
->targetMethod确定目标方法,确定获取目标Bean哪个方向的返回值
如果要获取静态方法的返回值,则无须指定targetObject,但是需要指定目标class,使用targetClass。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值