本文实例讲述了Spring实战之设置普通属性值的方法。分享给大家供大家参考,具体如下:
一 配置
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
二 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());
}
}
四 测试结果
1
2.3
希望本文所述对大家java程序设计有所帮助。