一 配置
二 Bean
1 Person
package org.crazyit.app.service;public class Person{ private int age; private Son son; // age的setter和getter方法 public void setAge(int age) { this.age = age; } public int getAge() { return this.age; } // son的setter和getter方法 public void setSon(Son son) { this.son = son; } public Son getSon() { return this.son; }}
2 Son
package org.crazyit.app.service;public class Son{ private int age; // age的setter和getter方法 public void setAge(int age) { this.age = age; } public int getAge() { return this.age; } public String toString() { return "Son[age=" + age + "]"; }}
三 测试类
package lee;import org.springframework.context.*;import org.springframework.context.support.*;import org.crazyit.app.service.*;public class SpringTest{ public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); System.out.println("系统获取的son1:" + ctx.getBean("son1")); System.out.println("系统获取son2:" + ctx.getBean("son2")); System.out.println("系统获取theAge的值:" + ctx.getBean("theAge")); System.out.println("系统获取theAge2的值:" + ctx.getBean("theAge2")); }}
四 测试结果
系统获取的son1:Son[age=11]系统获取son2:Son[age=11]系统获取theAge的值:11系统获取theAge2的值:30
上一篇:sql语句中怎么实现联合查询