分享Java快乐

我有一份快乐,分给你一些;我还是有一份快乐,你也有一份快乐。

mxj ID:javamxj
523443次访问,排名78好友0人,关注者16
javamxj的文章
原创 82 篇
翻译 0 篇
转载 0 篇
评论 720 篇
javamxj的公告

    版权声明:在此发表的有关文章均属本人javamxj原创,转摘或引用请注明出处。



联系方式:
Email:
javamxj@gmail.com

友情Blog

最近评论
linuxghs:删除configuration,eclipse不能启动了。郁闷
xinghui100:学习中
IvonXiao:谢谢博主的分享
xkpkhu:wow power leveling
xkpkhu:wow power leveling
文章分类
收藏
    相册
    有空逛逛
    java开源大全(RSS)
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 快速上手Spring--3. 加载Bean的配置文件收藏

    新一篇: Eclipse SDK 3.1来了 | 旧一篇: 快速上手Spring--2.HelloWorld(2)

    1. 创建项目
     
    · 新建一个Java Project:SpringBeanFile,注意要导入用户库Spring。
     
    · 这是完成后整个项目的结构(预览一下):
    · 项目源码下载(不包含库文件):http://free.ys168.com/?javamxj  Spring目录下面。
     
    2. 编写类文件
     
    · 下面开始创建一个新类:BeanFile ;包名:javamxj.spring.beanfile

    BeanFile.java

    package javamxj.spring.beanfile;

    public class BeanFile {

        private String beanFile = "多种方式加载Bean的配置文件";

        public void setBeanFile(String beanFile) {
            this.beanFile = beanFile;
        }

        public String getBeanFile() {
            return beanFile;
        }
    }
     
    · 新建Test.java,测试一下。

    Test.java

    package javamxj.spring.beanfile;

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.InputStream;

    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.support.BeanDefinitionRegistry;
    import org.springframework.beans.factory.support.DefaultListableBeanFactory;
    import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.core.io.FileSystemResource;
    import org.springframework.core.io.InputStreamResource;
    import org.springframework.core.io.Resource;

    public class Test {
        public static void main(String[] args) {

            // 直接调用HelloBean
            BeanFile bf = new BeanFile();
            System.out.println(bf.getBeanFile());

            /**
             * 利用XmlBeanFactory(Resource resource)
             * 这里Resource必须是xml格式
             * Resource包括:AbstractResource, ClassPathResource, FileSystemResource,
             * InputStreamResource, ServletContextResource, UrlResource
             */

            /*
             * 利用 InputStreamResource(InputStream inputStream)
             * 要将bean.xml放在项目根目录下
             */
            InputStream is = null;
            try {
                is = new FileInputStream("bean1.xml");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            Resource resource = new InputStreamResource(is);
            sayHello(resource);

            /*
             * 利用 ClassPathResource(String path)
             * 要将bean.xml放在源文件夹(src)目录下
             */
            resource = new ClassPathResource("bean2.xml");
            sayHello(resource);

            /*
             * 利用 FileSystemResource(String path)
             * 要将bean.xml放在项目根目录下
             */
            resource = new FileSystemResource("bean3.xml");
            sayHello(resource);

            /*
             * 利用 Properties
             * 要将bean.properties放在类路径--源文件夹(src)目录下
             */
            BeanDefinitionRegistry reg = new DefaultListableBeanFactory();
            PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(
                    reg);
            reader.loadBeanDefinitions(new ClassPathResource("bean.properties"));
            BeanFactory factory = (BeanFactory) reg;

            bf = (BeanFile) factory.getBean("beanFile");
            System.out.println("利用 " + bf.getBeanFile() + " 加载 Bean.properties");
           

            /*
             * 利用 ApplicationContext
             * 要将bean.xml放在类路径--源文件夹(src)目录下
             */
            ApplicationContext appContext = new ClassPathXmlApplicationContext(
                    "bean4.xml");
            bf = (BeanFile) appContext.getBean("beanFile");
            System.out.println("利用 " + bf.getBeanFile() + " 加载 Bean.xml");

        }

        public static void sayHello(Resource resource) {
            BeanFactory factory = new XmlBeanFactory(resource);
            BeanFile bf = (BeanFile) factory.getBean("beanFile");
            System.out.println("利用 " + bf.getBeanFile() + " 加载 Bean.xml");
        }
    }
     
     
     
    3. 配置文件
     
    由上面的Test.java可知,这里一共需要四个XML文件和一个Properties文件,现在分别建立。
     
    · bean1.xml放在项目根目录下:

    bean1.xml

    <?xml version="1.0" encoding="GBK"?>
    <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>
        <bean id="beanFile" class="javamxj.spring.beanfile.BeanFile">
            <property name="beanFile">
                <value>InputStreamResource(InputStream inputStream)</value>
            </property>
        </bean>
    </beans>
     
    bean2.xml、bean3.xml、bean4.xml与bean1.xml相似,仅仅需要替换一下<value>值即可。重要的注意文件的存放位置。这里只给出不同的代码;
     
    · bean2.xml放在源文件夹(src)目录下:

    bean2.xml(部分)

    <property name="beanFile">
         <value>ClassPathResource(String path)</value>
    </property>
     
    · bean3.xml放在项目根目录下:

    bean3.xml(部分)

    <property name="beanFile">
         <value>FileSystemResource(String path)</value>
    </property>
     
    · bean4.xml放在源文件夹(src)目录下:

    bean4.xml(部分)

    <property name="beanFile">
         <value>ApplicationContext</value>
    </property>
     
     
    Spring也可以使用属性文件来定义配置文件,如下:
    · bean.properties放在源文件夹(src)目录下:

    bean.properties

    beanFile.class=javamxj.spring.beanfile.BeanFile
    beanFile.beanFile=properties
     
     
    · 还需要将上文《快速上手Spring--2.HelloWorld(2)》中的log4j.properties复制到src目录下。
     
     
    4. 运行程序
     
    右击Test.java,运行程序,控制台输出如下:
     
    多种方式加载Bean的配置文件
    利用 InputStreamResource(InputStream inputStream) 加载 Bean.xml
    利用 ClassPathResource(String path) 加载 Bean.xml
    利用 FileSystemResource(String path) 加载 Bean.xml
    利用 properties 加载 Bean.properties
    利用 ApplicationContext 加载 Bean.xml

     
    5. 小结
     
       这篇文章主要谈论了如何加载Spring的配置文件,一般来说,就是BeanFactory和ApplicationContext。最常使用的、简单的BeanFactory实现是org.springframework.beans.factory.xml.XmlBeanFactory,其加载方式为: 
             BeanFactory factory = new XmlBeanFactory(Resource resource)
    这里resource必须是xml格式。Resource包括: AbstractResource, ClassPathResource,  FileSystemResource, InputStreamResource, ServletContextResource,   UrlResource。这篇文章 谈了常用的三种:ClassPathResource,  FileSystemResource, InputStreamResource。
          
          ApplicationContext包括了BeanFactory的所有功能,也要比BeanFactory强大的多(以后会详细介绍的)。这里只简单的使用了ClassPathXmlApplicationContext加载了Bean配置文件。你可以将log4j.properties中的“Warn”改为“Debug”, 对比一下和ClassPathResource的输出, 
      
       在Eclipse中,bean2.xml、bean4xml虽然都是放在源文件夹(src)目录下,但实际上,是由已经编译好的Test.class从类文件夹(这里是bin文件夹)中加载的。

    发表于 @ 2005年06月28日 17:03:00|评论(loading...)|编辑

    新一篇: Eclipse SDK 3.1来了 | 旧一篇: 快速上手Spring--2.HelloWorld(2)

    评论

    #wlf 发表于2005-06-29 18:33:00  IP: 61.186.252.*
    同谢
    #ld 发表于2005-06-28 21:45:00  IP: 61.186.252.*
    作者的认真、细致让人钦佩,对初学者很有帮助,谢谢!
    #小蚂蚁 发表于2005-07-13 16:35:00  IP: 61.186.252.*
    真是好东西啊,大哥,谢谢!!
    #pdf 发表于2005-10-19 17:51:00  IP: 211.100.21.*
    thank you!
    #freeman 发表于2005-10-24 15:35:00  IP: 211.100.21.*
    非常感谢!!!
    #zqh 发表于2005-11-04 16:43:00  IP: 219.137.157.*
    非常感谢!!!,期待你blog的更新!!!!
    #snow_man2004 发表于2005-12-10 12:48:00  IP: 219.129.250.*
    非常好,谢谢!
    #wantong 发表于2006-03-16 17:23:00  IP: 221.0.95.*
    挺好的,非常感谢!
    #happy2000 发表于2006-08-01 14:34:00  IP: 60.209.248.*
    在Eclipse中,bean2.xml、bean4xml虽然都是放在源文件夹(src)目录下,但实际上,是由已经编译好的Test.class从类文件夹(这里是bin文件夹)中加载的.
    这句话能解释吗?谢谢了.我的QQ:122198003.谢谢你.
    #mlm1009 发表于2007-06-08 15:30:49  IP: 58.59.135.*
    谢谢,学习了.不过,我运行了这个程序出现了这样的错.多种方式加载Bean的配置文件
    15:31:16,875 INFO CollectionFactory:76 - JDK 1.4+ collections available
    15:31:16,968 INFO XmlBeanDefinitionReader:347 - Loading XML bean definitions from resource loaded through InputStream
    Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Passed-in Resource [resource loaded through InputStream] contains an open stream: cannot determine validation mode automatically. Either pass in a Resource that is able to create fresh streams, or explicitly specify the validationMode on your XmlBeanDefinitionReader instance.
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.detectValidationMode(XmlBeanDefinitionReader.java:449)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.getValidationModeForResource(XmlBeanDefinitionReader.java:438)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:404)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:357)
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
    at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:73)
    at org.springframework.beans.factory.xml.XmlBeanFactory.<init>(XmlBeanFactory.java:61)
    at com.mlm.spring.Test.sayHello(Test.java:
    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © javamxj