证明spring中<property name="">这个双引号的内容只与setter方法有关,与一个类定义的字段和getter方法无关

证明如下:

思路定义两个实体类每个实体类的成员变量(字段)名和setter 和getter的名字都不一样:

原因是:bean的声明周期的原因:有一步是:注入属性。

其中一个类引用了另一个类。

被引用类的Address的代码如下:

 1 package com.timo.domain;
 2 
 3 public class Address {
 4     private String city6;
 5     private String state3;
 6 
 7     public String getCity4() {
 8         return city6;
 9     }
10 
11     public void setCity(String city) {
12         this.city6 = city;
13     }
14 
15     public String getState2() {
16         return state3;
17     }
18 
19     public void setState(String state) {
20         this.state3 = state;
21     }
22 }

引用类Student的代码如下:

 1 package com.timo.domain;
 2 
 3 public class Student {
 4     private Integer age;
 5     private String name;
 6     private Address address;
 7 
 8     public Integer getAge2() {
 9         return age;
10     }
11 
12     public void setAge(Integer age) {
13         this.age = age;
14     }
15 
16     public String getName2() {
17         return name;
18     }
19 
20     public void setName(String name) {
21         this.name = name;
22     }
23 
24     public Address getAddress() {
25         return address;
26     }
27 
28     public void setAddress(Address address) {
29         this.address = address;
30     }
31 
32     @Override
33     public String toString() {
34         return "Student{" +
35                 "age=" + age +
36                 ", name='" + name + '\'' +
37                 ", address=" + address +
38                 '}';
39     }
40 }

配置文件.xml的代码如下:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans
 5                 http://www.springframework.org/schema/beans/spring-beans.xsd">
 6 
 7     <bean id="address" class="com.timo.domain.Address">
 8         <!-- collaborators and configuration for this bean go here -->
 9         <property name="city" value="合肥市"/>
10         <property name="state" value="安徽省"/>
11     </bean>
12 
13     <bean id="student" class="com.timo.domain.Student">
14         <!-- collaborators and configuration for this bean go here -->
15         <property name="address" ref="address"/>
16         <property name="age" value="18"/>
17         <property name="name" value="欧阳凤"/>
18       </bean>
19 
20     <!-- more bean definitions go here -->
21 
22 </beans>

测试类Test的代码如下:

package com.timo.test;

import com.timo.domain.Student;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        String city = applicationContext.getBean(Student.class).getAddress().getCity4();
        System.out.println("she inhabit in :"+city);
    }
}

posted @ 2017-11-22 09:22  技术让世界更精彩  阅读(383)  评论(0编辑  收藏  举报