在讀spring in aciton 時(shí),他用的BeanFactory factory = new XmlBeanFactory(new FileInputStream("hello.xml"));
可是現(xiàn)在的用的1.2.6版本的構(gòu)造器(XmlBeanFactory)只能接收Resource接口了,所以調(diào)不出來(lái)是正常的事情,假設(shè)現(xiàn)在有一個(gè)文件hello.xml
讀取方法
1:ApplicationContext cx=new FileSystemXmlApplicationContext("hello.xml");//指定的路徑去找文件
2:ApplicationContext factory = new ClassPathXmlApplicationContext("hello.xml");//還會(huì)在classpath去找
3:Resource fa = new FileSystemResource("hello.xml");
? ?BeanFactory factory=new XmlBeanFactory(fa);
4:這個(gè)要設(shè)制classpath了,麻煩
?Resource res = new ClassPathResource("com/springinaction/chapter01/hello/hello.xml");
?BeanFactory factory=new XmlBeanFactory(res);
好了,用了上面那種方法都可以調(diào)用getBean("your bean name")了,
eg:?BeanFactory factory=new XmlBeanFactory(fa);
????? hello he=(hello)factory.getBean("hello");
????????????? he.getHello();