探索与发现

研究java技术

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  83 随笔 :: 0 文章 :: 109 评论 :: 0 Trackbacks
在读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-03 11:24 蜘蛛 阅读(2643) 评论(1)  编辑  收藏 所属分类: spring

评论

# re: 读取配置文件的几种方法 2007-10-13 13:37 很好
很不错,顶!!!!!!!  回复  更多评论
  


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


网站导航: