如何将城市的属性值从地址 Bean 设置为人 Bean 的城市名称属性

  • 本文关键字:Bean 属性 城市 设置 地址 spring
  • 更新时间 :
  • 英文 :


My 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"
           xmlns:util="http://www.springframework.org/schema/util"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans 
                               http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean  id="add" class="com.basu.Address">
    <property name="H_NO" value="7"/>
    <property name="city" value="bellary"/>
    <property name="state"  value="karnataka"/>
    </bean>
    <bean id="per" class="com.basu.person" >
    <property name="cityname" value="#{add.city}"/> 
    </bean>
</beans>

我想要 ouput(地址豆) :-

7
bellary
karnataka

我想要 ouput(人豆) :-

bellary

假设您的getter和setter被相应地命名,您拥有的东西是完全可以的。

Address

public class Address {
    private String H_NO;
    private String city;
    private String state;
    public String getH_NO() {
        return H_NO;
    }
    public void setH_NO(String h_NO) {
        H_NO = h_NO;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getState() {
        return state;
    }
    public void setState(String state) {
        this.state = state;
    }
}

person

public class person {
    private String cityname;
    public String getCityname() {
        return cityname;
    }
    public void setCityname(String cityname) {
        this.cityname = cityname;
    }
}

请记住,Spring bean 遵循 Java Bean 的命名约定。您还应该遵循 Java 命名约定。类名应以大写字母开头,并使用 CamelCaseH_NO描述性不强。使用homeNumber或类似的东西。

相关内容

最新更新