Spring的非XML格式bean声明

转帖|其它|编辑:郝浩|2008-12-18 11:48:19.000|阅读 1424 次

概述: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@evget.com

文章转载自:DIY部落

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP