Spring+Maven学习实验- Spring 中给 Bean 属性注入value(二)

1.修改pom.xml

   详细请看实验一

2.创建Person和Customer类

package com.shiyanlou.spring.innerbean;

public class Person {
    private String name;
    private String address;
    private int age;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
    @Override
    public String toString() {
        return "Person [name=" + name + ", address=" + address + ", age=" + age + "]";
    }
    
    

}
package com.shiyanlou.spring.innerbean;

public class Customer {
    private Person person;
    //带参构造方法
    public Customer(Person person){
        this.person = person;
    }
    public Customer() {}
    
    public Person getPerson() {
        return person;
    }
    public void setPerson(Person person) {
        this.person = person;
    }
    @Override
    public String toString() {
        return "Customer [person=" + person + "]";
    }
    
    
}

3.创建Customerbeans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
    <!--方法一:利用 ref 很好的引用了 Person ,但是,一旦 Person 被用在 Customer 下,也就不会被别的 Bean 引用-->
    <!--  
    <bean id="CustomerBean" class="com.shiyanlou.spring.innerbean.Customer">
       <property name="Person" ref="PersonBean"/>
    </bean>
    
    <bean id="Person" class="com.shiyanlou.spring.innerbean.Person">
       <property name="name" value="zoey"/>
       <property name="address" value="Wuhan"/>
       <property name="age" value="27"/>
    </bean>
   -->
   <!-- 方法二:在 Customer 的 Bean 中声明一个内部 Bean  -->
   <bean id="Customer" class="com.shiyanlou.spring.innerbean.Customer">
       <property name="person">
          <bean class="com.shiyanlou.spring.innerbean.Person">
             <property name="name" value="zoey"/>
             <property name="address" value="Wuhan"/>
             <property name="age" value="27"/>
          </bean>
       </property>   
   </bean>
   
   <!-- 内部 Bean 也可以通过构造函数注入 -->
   <!-- 
       <bean id="Customer" class="com.shiyanlou.spring.innerBean.Customer">
        <constructor-arg>
            <bean class=com.shiyanlou.spring.innerBean.Person">
                <property name="name" value="shiyanlou" />
                <property name="address" value="chengdu" />
                <property name="age" value="25" />
            </bean>
        </constructor-arg>
       </bean>
    -->
</beans>

 

4.创建测试类app.java

package com.shiyanlou.spring.innerbean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    private static ApplicationContext context;

    public static void main(String[] args) {
        context = new ClassPathXmlApplicationContext("CustomerBeans.xml");
        Customer customer = (Customer)context.getBean("Customer"); //Customer为Customerbeans.xml中的bean id
        //customer.toString();
        System.out.println(customer.toString());

    }

}

运行App.java后效果

 

posted @ 2017-03-14 11:08  Zoey Chou  阅读(684)  评论(0编辑  收藏  举报