1. 程式人生 > >Spring(20) 獲取別的bean的屬性

Spring(20) 獲取別的bean的屬性

  1. 嗯。。。就是在配置檔案中一個bean把別人的屬性拿過來用
  2. 通過將class指定為PropertyFactoryBean來實現獲取其他bean的屬性
  3. 在程式碼裡邊說吧,總而言之呢就是把class指定為ProperFactory,然後再指定是要獲取那個bean的哪個屬性
    package InstancePackage;
    
    public class Person {
        int age;
    
        Son son;
    
        public Son getSon() {
            return son;
        }
    
        public void setSon(Son son) {
            this.son = son;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    
    package InstancePackage;
    
    public class Son {
        int age;
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    
    package TestPackage;
    
    import InstancePackage.Person;
    import InstancePackage.Son;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class SpringTest {
        public static void main(String []args){
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
            Son son = applicationContext.getBean("son1", Son.class);
            System.out.println(son.getAge());
    
    
            Son son1 = applicationContext.getBean("son2", Son.class);
            System.out.println(son1.getAge());
    
            int  age1 = applicationContext.getBean("theAge", Integer.class);
            System.out.println(age1);
    
            int age2 = applicationContext.getBean("theAge2", Integer.class);
            System.out.println(age2);
        }
    }
    
    <?xml version="1.0" encoding="GBK"?>
    <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://www.springframework.org/schema/beans"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
    
    
        <!--這個是一個普通的bean-->
        <bean id="person" class="InstancePackage.Person">
            <property name="age" value="31"/>
            <property name="son">
                <bean class="InstancePackage.Son">
                    <property name="age" value="10"/>
                </bean>
            </property>
        </bean>
    
    <!--這裡將class指定為PropertyPathFactoryBean,然後呢,用targetBeanName指定要獲取哪一個bean的屬性
    用propertyPath指定要獲取它的哪一個屬性,就ok-->
        <bean id="son1" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
            <property name="targetBeanName" value="person"/>
            <property name="propertyPath" value="son"/>
        </bean>
    
    <!--這個呢它的實現類是一個son,但是它的age屬性id為person.son.age,表示是要獲取person的son的
    age屬性值,當然啦class是PropertyPathFactoryBean-->
     <bean id="son2" class="InstancePackage.Son">
         <property name="age" >
             <bean id="person.son.age"
                   class="org.springframework.beans.factory.config.PropertyPathFactoryBean"/>
         </property>
     </bean>
    
    <!--這裡呢是最終是一個int,因為它的目標是person下的son的age-->
        <bean id="theAge" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
            <property name="targetBeanName" value="person"/>
            <property name="propertyPath" value="son.age"/>
        </bean>
    
    
        <!--這裡是自己定義了一個目標bean,然後獲取它的age-->
        <bean id="theAge2" class="org.springframework.beans.factory.config.PropertyPathFactoryBean">
            <property name="targetObject">
                <bean class="InstancePackage.Person">
                    <property name="age" value="30"/>
                </bean>
            </property>
    
            <property name="propertyPath" value="age"/>
        </bean>
    
    </beans>

    這是我看李剛編著的《輕量級javaEE企業應用實戰(第五版)-Struts2+Spring5+Hibernate5/JAP2》後總結出來的。