美文网首页
4.如何在一个实体类中注入另一个实体类

4.如何在一个实体类中注入另一个实体类

作者: 小竹猫 | 来源:发表于2018-12-21 11:32 被阅读0次

    实体类Person

    public class Person {
        private String name;
        private int age;
        //类的关联关系 ...关联,继承
        //Pet pet = new Pet();耦合
        private Pet pet;
        public Person(String name, int age, Pet pet) {
            super();
            this.name = name;
            this.age = age;
            this.pet = pet;
        }
        public Person() {
            super();
            // TODO Auto-generated constructor stub
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public Pet getPet() {
            return pet;
        }
        public void setPet(Pet pet) {
            this.pet = pet;
        }
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + ", pet=" + pet + "]";
        }
        
        
    }
    

    实体类Pet

    public class Pet {
        private String petName;
        private int age;
        public String getPetName() {
            return petName;
        }
        public void setPetName(String petName) {
            this.petName = petName;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        @Override
        public String toString() {
            return "Pet [petName=" + petName + ", age=" + age + "]";
        }
        
        
    }
    

    可以看到在Person类中,有Pet pet 这样一个属性,那么如何给它注入属性?
    可以通过ref标签

    ref标签 引用或指定
    1.ref="pet1" 第一种写法.
    2.<ref bean="pet1"/>

    <bean id="p1" class="com.igeek.lesson4.Person">
            <property name="name" value="小明"></property>
            <property name="age" value="20"></property>
            <property name="pet">
                <ref bean="pet1"/>
            </property>
        </bean>
        
        <bean id="pet1" class="com.igeek.lesson4.Pet">
            <property name="petName" value="小老虎"></property>
            <property name="age" value="20"></property>
        </bean>
    

    也可以:

    <bean id="p2" class="com.igeek.lesson4.Person">
            <property name="name" value="张全蛋"></property>
            <property name="age" value="38"></property>
            <property name="pet">
                <bean class="com.igeek.lesson4.Pet">
                    <property name="petName" value="小海腾"></property>
                    <property name="age" value="2"></property>
                </bean>
            </property>
        </bean>
    

    还可以通过一种更为复杂的方式:

    <bean id="timo" class="com.igeek.lesson4.Pet"></bean>
        <bean id="p3" class="com.igeek.lesson4.Person">
            <constructor-arg name="name">
                <null/>
            </constructor-arg>
            <constructor-arg name="age" value="18"></constructor-arg>
            <constructor-arg name="pet" ref="timo"></constructor-arg>
            <!-- 为级联属性赋值 -->
            <property name="pet.petName" value="提莫"></property>
            <property name="pet.age" value="20"></property>
        </bean>
    

    相关文章

      网友评论

          本文标题:4.如何在一个实体类中注入另一个实体类

          本文链接:https://www.haomeiwen.com/subject/vxlmkqtx.html