Junky's IT Notebook

统计

留言簿(8)

积分与排名

WebSphere Studio

阅读排行榜

评论排行榜

读取配置文件的几种方法

在读spring in aciton 时,他用的BeanFactory factory = new XmlBeanFactory(new FileInputStream("hello.xml"));
可是现在的用的1.2.6版本的构造器(XmlBeanFactory)只能接收Resource接口了,所以调不出来是正常的事情,假设现在有一个文件hello.xml
读取方法

1:ApplicationContext cx=new FileSystemXmlApplicationContext("hello.xml");//指定的路径去找文件
2:ApplicationContext factory = new ClassPathXmlApplicationContext("hello.xml");//还会在classpath去找
3:Resource fa = new FileSystemResource("hello.xml");
   BeanFactory factory=new XmlBeanFactory(fa);
4:这个要设制classpath了,麻烦
  Resource res = new ClassPathResource("com/springinaction/chapter01/hello/hello.xml");
  BeanFactory factory=new XmlBeanFactory(res);
好了,用了上面那种方法都可以调用getBean("your bean name")了,
eg: BeanFactory factory=new XmlBeanFactory(fa);
      hello he=(hello)factory.getBean("hello");
              he.getHello();

posted on 2006-06-04 23:27 junky 阅读(311) 评论(0)  编辑  收藏 所属分类: java


只有注册用户登录后才能发表评论。


网站导航: