每个对象必然是有诸多属性的,那么给属性赋值,自然也是至关重要的,好在bean能够完成这一点
property:通过setter对应的方法注入。
name 表示的是set方法的名字 . 去掉set 首写字符小写
<bean id="tom" class="com.igeek.lesson2.Person">
<property name="name" value="汤姆"></property>
<property name="age" value="18"></property>
</bean>
ApplicationContext ac = new ClassPathXmlApplicationContext("com/igeek/lesson2/lesson2.xml");
//这种形式只有在配置bean单个的时候可行
Person bean = ac.getBean(Person.class);
System.out.println(bean);
输出结果:
Person [name=汤姆, age=18]
网友评论