Spring實(shí)戰(zhàn)之設(shè)置普通屬性值的方法示例
本文實(shí)例講述了Spring實(shí)戰(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
{
// 定義一個(gè)int型的成員變量
private int integerField;
// 定義一個(gè)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;
}
}
三 測(cè)試類(lèi)
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)
{
//以類(lèi)加載路徑下的bean.xml文件來(lái)創(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());
}
}
四 測(cè)試結(jié)果
1
2.3
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Spring框架入門(mén)與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- Spring實(shí)戰(zhàn)之屬性占位符配置器用法示例
- Spring Boot環(huán)境屬性占位符解析及類(lèi)型轉(zhuǎn)換詳解
- 解決Spring國(guó)際化文案占位符失效問(wèn)題的方法
- Spring MVC 框架搭建配置方法及詳解
- 詳解SpringBoot配置連接池
- spring boot Logging的配置以及使用詳解
- Spring實(shí)戰(zhàn)之獲取其他Bean的屬性值操作示例
- SpringBoot沒(méi)有主清單屬性的解決方法
- spring為類(lèi)的靜態(tài)屬性實(shí)現(xiàn)注入實(shí)例方法
- Spring實(shí)戰(zhàn)之屬性覆蓋占位符配置器用法示例
相關(guān)文章
java核心編程之文件過(guò)濾類(lèi)FileFilter和FilenameFilter
這篇文章主要為大家詳細(xì)介紹了java文件過(guò)濾類(lèi)FileFilter和FilenameFilter,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
java基于jdbc實(shí)現(xiàn)簡(jiǎn)單學(xué)生管理系統(tǒng)
本文主要主要介紹了java連接mysql數(shù)據(jù)庫(kù)的一個(gè)簡(jiǎn)單學(xué)生系統(tǒng),通過(guò)jdbc連接數(shù)據(jù)庫(kù)。文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
啟用springboot security后登錄web頁(yè)面需要用戶名和密碼的解決方法
這篇文章主要介紹了啟用springboot security后登錄web頁(yè)面需要用戶名和密碼的解決方法,也就是使用默認(rèn)用戶和密碼登錄的操作方法,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02
Java代理的幾種實(shí)現(xiàn)方式總結(jié)
本文將通過(guò)例子說(shuō)明java代理的幾種實(shí)現(xiàn)方式,并比較它們之間的差異,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的參考價(jià)值,需要的朋友可以參考下2023-12-12
基于Spring + Spring MVC + Mybatis 高性能web構(gòu)建實(shí)例詳解
這篇文章主要介紹了基于Spring + Spring MVC + Mybatis 高性能web構(gòu)建實(shí)例詳解,需要的朋友可以參考下2017-04-04
spring boot+ redis 接口訪問(wèn)頻率限制的實(shí)現(xiàn)
這篇文章主要介紹了spring boot+ redis 接口訪問(wèn)頻率限制的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
springboot?實(shí)戰(zhàn):異常與重定向問(wèn)題
這篇文章主要介紹了springboot實(shí)戰(zhàn):異常與重定向問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12

