Spring實戰(zhàn)之設(shè)置普通屬性值的方法示例
本文實例講述了Spring實戰(zhàn)之設(shè)置普通屬性值的方法。分享給大家供大家參考,具體如下:
一 配置
<?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型的參數(shù)值 -->
<property name="integerField" value="1"/>
<!-- 指定double型的參數(shù)值 -->
<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文件來創(chuàng)建Spring容器
ApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
ExampleBean b = ctx.getBean("exampleBean"
, ExampleBean.class);
System.out.println(b.getIntegerField());
System.out.println(b.getDoubleField());
}
}
四 測試結(jié)果
1
2.3
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
java核心編程之文件過濾類FileFilter和FilenameFilter
這篇文章主要為大家詳細介紹了java文件過濾類FileFilter和FilenameFilter,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08
java基于jdbc實現(xiàn)簡單學生管理系統(tǒng)
本文主要主要介紹了java連接mysql數(shù)據(jù)庫的一個簡單學生系統(tǒng),通過jdbc連接數(shù)據(jù)庫。文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
啟用springboot security后登錄web頁面需要用戶名和密碼的解決方法
這篇文章主要介紹了啟用springboot security后登錄web頁面需要用戶名和密碼的解決方法,也就是使用默認用戶和密碼登錄的操作方法,本文結(jié)合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-02-02
基于Spring + Spring MVC + Mybatis 高性能web構(gòu)建實例詳解
這篇文章主要介紹了基于Spring + Spring MVC + Mybatis 高性能web構(gòu)建實例詳解,需要的朋友可以參考下2017-04-04
spring boot+ redis 接口訪問頻率限制的實現(xiàn)
這篇文章主要介紹了spring boot+ redis 接口訪問頻率限制的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01

