现在的位置: 首页 > 综合 > 正文

7.5.4:设置普通属性值

2013年10月10日 ⁄ 综合 ⁄ 共 595字 ⁄ 字号 评论关闭

<value.../>元素用于指定字符串类型、基本类型的属性值。

Person.java :

public class Person{
	
	private String name;
	private int age;
	
	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 Person() {
		
	}
	
}

bean.xml 核心配置:

<bean id="p" class="com.bean.Person">
        <property name="name" value="tom"/>
        <property name="age" value="23"/>
</bean>

Test.java :

public class Test {

	public static void main(String[] args) {
		
		ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");
		Person p=(Person) ctx.getBean("p");
		System.out.println(p.getName()+" "+p.getAge());//tom 23
	}

}

抱歉!评论已关闭.