1.利用ClassPathXmlApplicationContext,这种方式配置文件应该放在类包同路径下
Java代码:ApplicationContext ct=newClassPathXmlApplicationContext("applicationContext01.xml");
ct.getBean("");

2.利用FileSystemXmlApplicationContext,这种方法可以将配置文件放在工程的直接目录下
Java代码:
ApplicationContext ct=newFileSystemXmlApplicationContext("applicationContext01.xml");
ct.getBean("");

3.显式给出配置文件的绝对路径,我们假定配置文件位于工程主目录中的conf文件夹中
Java代码:
String path=System.getProperty("user.dir");
ApplicationContext ct=newFileSystemXmlApplicationContext(path+"//conf//applicationContext01.xml");
ct.getBean("");

4.利用FileSystemResource,可以将配置文件放在工程直接目录下
Java代码:
Resource rs=newFileSystemResource("applicationContext01.xml");
BeanFactory ct=newXmlBeanFactory(rs);
ct.getBean("");

5.显式给出配置文件的绝对路径,我们假定配置文件位于工程主目录中的conf文件夹中
Java代码:
String path=System.getProperty("user.dir");
Resource rs=newFileSystemResource(path+"//conf//applicationContext01.xml");
BeanFactory ct=newXmlBeanFactory(rs);
ct.getBean("");

网友评论