一 配置
二 Bean
package org.crazyit.app.service;public class ExampleBean{ // 定义一个int型的成员变量 private int integerField; // 定义一个double型的成员变量 private double doubleField; // integerField的setter和getter方法 public void setIntegerField(int integerField) { this.integerField = integerField; } public int getIntegerField() { return this.integerField; } // doubleField的setter和getter方法 public void setDoubleField(double doubleField) { this.doubleField = doubleField; } public double getDoubleField() { return this.doubleField; }}
三 测试类
package lee;import org.springframework.context.*;import org.springframework.context.support.*;import org.crazyit.app.service.*;public class BeanTest{ public static void main(String[] args) { //以类加载路径下的bean.xml文件来创建Spring容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); ExampleBean b = ctx.getBean("exampleBean" , ExampleBean.class); System.out.println(b.getIntegerField()); System.out.println(b.getDoubleField()); }}
四 测试结果
12.3