1.利用ClassPathXmlApplicationC ontext
可以从classpath中读取XML文件
(1)
ApplicationContext context = new ClassPathXmlApplicationC ontext("applicationContext.xml"); UserDao userDao = (UserDao)context.getBean("userDao");
(2)
ClassPathXmlApplicationC ontext resource = new ClassPathXmlApplicationC ontext(new String[]{"applicationContext-ibatis-oracle.xml","applicationContext.xml","applicationContext-data-oracle.xml"}); BeanFactory factory = resource; UserDao userDao = (UserDao) factory.getBean("userDao");
2. 利用ClassPathResource
可以从classpath中读取XML文件
Resource cr = new ClassPathResource("applicationContext.xml"); BeanFactory bf=new XmlBeanFactory(cr); UserDao userDao = (UserDao)bf.getBean("userDao");
加载一个xml文件org.springframework.beans.factory.config.PropertyPlaceholderConfi gurer不起作用
3.利用XmlWebApplicationContext 读取
XmlWebApplicationContext ctx = new XmlWebApplicationContext (); ctx.setConfigLocations(new String[] {"/WEB-INF/ applicationContext.xml"); ctx.setServletContext(pageContext.getServletContext()); ctx.refresh(); UserDao userDao = (UserDao ) ctx.getBean("userDao ");
4.利用FileSystemResource读取
Resource rs = new FileSystemResource("D:/tomcat/webapps/test/WEB-INF/classes/ applicationContext.xml"); BeanFactory factory = new XmlBeanFactory(rs); UserDao userDao = (UserDao )factory.getBean("userDao");
值得注意的是:利用FileSystemResource,则配置文件必须放在project直接目录下,或者写明绝对路径,否则就会抛出找不到文件的异常。
5.利用FileSystemXmlApplication Context读取
可以指定XML定义文件的相对路径或者绝对路径来读取定义文件。
方法一:
String[] path={"WebRoot/WEB-INF/applicationContext.xml","WebRoot/WEB-INF/applicationContext_task.xml"}; ApplicationContext context = new FileSystemXmlApplication Context(path);
方法二:
String path="WebRoot/WEB-INF/applicationContext*.xml"; ApplicationContext context = new FileSystemXmlApplication Context(path);
方法三:
ApplicationContext ctx = new FileSystemXmlApplication Context("classpath:地址");
没有classpath的话就是从当前的工作目录
可以从classpath中读取XML文件
(1)
ApplicationContext context = new ClassPathXmlApplicationC
(2)
ClassPathXmlApplicationC
2. 利用ClassPathResource
可以从classpath中读取XML文件
Resource cr = new ClassPathResource("applicationContext.xml"); BeanFactory bf=new XmlBeanFactory(cr); UserDao userDao = (UserDao)bf.getBean("userDao");
加载一个xml文件org.springframework.beans.factory.config.PropertyPlaceholderConfi
3.利用XmlWebApplicationContext
XmlWebApplicationContext
4.利用FileSystemResource读取
Resource rs = new FileSystemResource("D:/tomcat/webapps/test/WEB-INF/classes/ applicationContext.xml"); BeanFactory factory = new XmlBeanFactory(rs); UserDao userDao = (UserDao )factory.getBean("userDao");
值得注意的是:利用FileSystemResource,则配置文件必须放在project直接目录下,或者写明绝对路径,否则就会抛出找不到文件的异常。
5.利用FileSystemXmlApplication
可以指定XML定义文件的相对路径或者绝对路径来读取定义文件。
方法一:
String[] path={"WebRoot/WEB-INF/applicationContext.xml","WebRoot/WEB-INF/applicationContext_task.xml"}; ApplicationContext context = new FileSystemXmlApplication
方法二:
String path="WebRoot/WEB-INF/applicationContext*.xml"; ApplicationContext context = new FileSystemXmlApplication
方法三:
ApplicationContext ctx = new FileSystemXmlApplication
没有classpath的话就是从当前的工作目录