轉帖|其它|編輯:郝浩|2008-12-18 11:48:19.000|閱讀 1433 次
概述:Spring的非XML格式bean聲明
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
文章關鍵字:|Spring|XML|bean|java|測試|
spring除了支持XML格式的bean聲明外,還對屬性文件格式和編程方式注冊也提供了支持。
首先先創建一個用于測試的bean對象的java文件,TextStyle.java。
package lm.java.spring.bean.factory.test;
public class TextStyle {
private String fontName = "default";
private int fontSize = 9;
private boolean bold = false;
private boolean italic = false;
public String getFontName() {
return fontName;
}
public void setFontName(String fontName) {
this.fontName = fontName;
}
public int getFontSize() {
return fontSize;
}
public void setFontSize(int fontSize) {
this.fontSize = fontSize;
}
public boolean isBold() {
return bold;
}
public void setBold(boolean bold) {
this.bold = bold;
}
public boolean isItalic() {
return italic;
}
public void setItalic(boolean italic) {
this.italic = italic;
}
}
一、對于讀取屬性文件這種bean聲明,使用org.springframework.beans.factory.support.PropertiesBeanDefinitionReader裝載bean聲明。
例:
1.在ClASSPATH下創建一個styles.properties文件。
myStyle.class=lm.java.spring.bean.factory.test.TextStyle
myStyle.fontName=Arial
myStyle.fontSize=12
yourStyle.class=lm.java.spring.bean.factory.test.TextStyle
yourStyle.fontName=Times
yourStyle.fontSize=10
yourStyle.bold=true
yourStyle.italic=true[SPAN]
2.創建一個PropertiesBeanFactoryTest測試類。
package lm.java.spring.bean.factory.test;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class PropertiesBeanFactoryTest {
public static void main(String[] args) {
// ClassPathResource的默認路徑是工程下面的bin文件夾為根路徑
Resource resource = new ClassPathResource("lm\\java\\spring\\bean\\factory\\test\\styles.properties");
// 使用DefaultListableBeanFactory和PropertiesBeanDefinitionReader配置bean工廠
DefaultListableBeanFactory dbf = new DefaultListableBeanFactory();
PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(dbf);
reader.loadBeanDefinitions(resource);
TextStyle dfmyStyle = (TextStyle) dbf.getBean("myStyle");
System.out.println(dfmyStyle.getFontName() + " , " +
dfmyStyle.getFontSize() + " , " + dfmyStyle.isBold() + "," +
dfmyStyle.isItalic());
TextStyle dbfyourStyle = (TextStyle) dbf.getBean("yourStyle");
System.out.println(dbfyourStyle.getFontName() + " , " +
dbfyourStyle.getFontSize() + " , " + dbfyourStyle.isBold() + "," +
dbfyourStyle.isItalic());
}
}
運行后的結果
Arial , 12 , false,false
Times , 10 , true,true
二、對于編程方式注冊bean聲明,可以使用Spring提供的org.springframework.beans.support.RootBeanDefinition類和org.springframework.beans.MutablePropertyValues類協作裝載bean聲明。
例:創建一個RootBeanDefinitionTest測試類。
package lm.java.spring.bean.factory.test;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
public class RootBeanDefinitionTest {
public static void main(String[] args) {
DefaultListableBeanFactory dbf = new DefaultListableBeanFactory();
MutablePropertyValues myStylePv = new MutablePropertyValues();
myStylePv.addPropertyValue("fontName", "Arial");
myStylePv.addPropertyValue("fontSize", "12");
RootBeanDefinition myStyleRbd = new RootBeanDefinition(TextStyle.class, myStylePv);
dbf.registerBeanDefinition("myStyle", myStyleRbd);
MutablePropertyValues yourStylePv = new MutablePropertyValues();
myStylePv.addPropertyValue("fontName", "Times");
myStylePv.addPropertyValue("fontSize", "10");
myStylePv.addPropertyValue("bold", "true");
myStylePv.addPropertyValue("italic", "true");
RootBeanDefinition yourStyleRbd = new RootBeanDefinition(TextStyle.class, yourStylePv);
dbf.registerBeanDefinition("yourStyle", yourStyleRbd);
TextStyle dfmyStyle = (TextStyle) dbf.getBean("myStyle");
System.out.println(dfmyStyle.getFontName() + " , " +
dfmyStyle.getFontSize() + " , " + dfmyStyle.isBold() + "," +
dfmyStyle.isItalic());
TextStyle dbfyourStyle = (TextStyle) dbf.getBean("yourStyle");
System.out.println(dbfyourStyle.getFontName() + " , " +
dbfyourStyle.getFontSize() + " , " + dbfyourStyle.isBold() + "," +
dbfyourStyle.isItalic());
}
}
運行后的結果
Arial , 12 , false,false
Times , 10 , true,true
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:DIY部落