文章关键字:|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
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
未完,原文地址:http://www.evget.com/zh-CN/Info/ReadInfo.aspx?id=9172
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
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
未完,原文地址:http://www.evget.com/zh-CN/Info/ReadInfo.aspx?id=9172