zoukankan      html  css  js  c++  java
  • 加载SpringContext文件的方式

    1.利用ClassPathXmlApplicationContext

    可以从classpath中读取XML文件
    (1)
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao)context.getBean("userDao");

    (2)
    ClassPathXmlApplicationContext resource = new ClassPathXmlApplicationContext(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.PropertyPlaceholderConfigurer不起作用

    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.利用FileSystemXmlApplicationContext读取

    可以指定XML定义文件的相对路径或者绝对路径来读取定义文件。

    方法一:
    String[] path={"WebRoot/WEB-INF/applicationContext.xml","WebRoot/WEB-INF/applicationContext_task.xml"}; ApplicationContext context = new FileSystemXmlApplicationContext(path);

    方法二:
    String path="WebRoot/WEB-INF/applicationContext*.xml"; ApplicationContext context = new FileSystemXmlApplicationContext(path);

    方法三:
    ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:地址");

    没有classpath的话就是从当前的工作目录


    文章出自:http://www.cnblogs.com/secret1998/archive/2010/05/24/1742567.html

  • 相关阅读:
    375D.Tree and Queries(树上启发式合并+离线)
    600E.Lomsat gelral (树上启发式合并)
    741D.Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(树上启发式合并+状压)
    Nowcoder13249.黑白树(树形DP)
    Nowcoder51179.选课(树形背包)
    Nowcoder20811.蓝魔法师(树形背包)
    Nowcoder19782.Tree(树形DP+逆元)
    Gym102292M.Monster Hunter(树形背包+滚动数组)
    大数据运维(61)Linux环境安装PostgreSQL-10.1
    大数据运维(60)Hive on Spark配置
  • 原文地址:https://www.cnblogs.com/pocter/p/3684488.html
Copyright © 2011-2022 走看看