在讀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();