7.5.4:设置普通属性值

来源:互联网 发布:南京seo顾问 编辑:程序博客网 时间:2024/05/07 17:35

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



原创粉丝点击