<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 } }