Spring實戰之設定普通屬性值的方法示例
阿新 • • 發佈:2020-01-07
本文例項講述了Spring實戰之設定普通屬性值的方法。分享給大家供大家參考,具體如下:
一 配置
<?xml version="1.0" encoding="GBK"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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 id="exampleBean" class="org.crazyit.app.service.ExampleBean"> <!-- 指定int型的引數值 --> <property name="integerField" value="1"/> <!-- 指定double型的引數值 --> <property name="doubleField" value="2.3"/> </bean> </beans>
二 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相關內容感興趣的讀者可檢視本站專題:《Spring框架入門與進階教程》、《Java資料結構與演算法教程》、《Java操作DOM節點技巧總結》、《Java檔案與目錄操作技巧彙總》和《Java快取操作技巧彙總》
希望本文所述對大家java程式設計有所幫助。