zoukankan      html  css  js  c++  java
  • Spring中加载xml配置文件的六种方式

    因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装入系统,这就需要利用Spring去动态加载某一位置下的配置文件,所以就总结了下Spring中加载xml配置文件的方式,我总结的有6种, xml是最常见的spring 应用系统配置源。Spring中的几种容器都支持使用xml装配bean,包括: 
    XmlBeanFactory,ClassPathXmlApplicationContext,FileSystemXmlApplicationContext,XmlWebApplicationContext 

    一: XmlBeanFactory 引用资源 
    Resource resource = new ClassPathResource("appcontext.xml"); 
    BeanFactory factory = new XmlBeanFactory(resource); 

    二: ClassPathXmlApplicationContext  编译路径 
    ApplicationContext factory=new ClassPathXmlApplicationContext("classpath:appcontext.xml"); 
    // src目录下的 
    ApplicationContext factory=new ClassPathXmlApplicationContext("appcontext.xml"); 
    ApplicationContext factory=new ClassPathXmlApplicationContext(new String[] {"bean1.xml","bean2.xml"}); 
    // src/conf 目录下的 
    ApplicationContext factory=new ClassPathXmlApplicationContext("conf/appcontext.xml"); 
    ApplicationContext factory=new ClassPathXmlApplicationContext("file:G:/Test/src/appcontext.xml"); 

    三: 用文件系统的路径 
    ApplicationContext factory=new FileSystemXmlApplicationContext("src/appcontext.xml"); 
    //使用了  classpath:  前缀,作为标志,  这样,FileSystemXmlApplicationContext 也能够读入classpath下的相对路径 
    ApplicationContext factory=new FileSystemXmlApplicationContext("classpath:appcontext.xml"); 
    ApplicationContext factory=new FileSystemXmlApplicationContext("file:G:/Test/src/appcontext.xml"); 
    ApplicationContext factory=new FileSystemXmlApplicationContext("G:/Test/src/appcontext.xml"); 

    四: XmlWebApplicationContext是专为Web工程定制的。 
    ServletContext servletContext = request.getSession().getServletContext(); 
    ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext ); 

    五: 使用BeanFactory 
    BeanDefinitionRegistry reg = new DefaultListableBeanFactory(); 
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(reg); 
    reader.loadBeanDefinitions(new ClassPathResource("bean1.xml")); 
    reader.loadBeanDefinitions(new ClassPathResource("bean2.xml")); 
    BeanFactory bf=(BeanFactory)reg; 

    六:Web 应用启动时加载多个配置文件 
    通过ContextLoaderListener 也可加载多个配置文件,在web.xml文件中利用 
    <context-pararn>元素来指定多个配置文件位置,其配置如下: 

    Java代码 
    1. <context-param>  
    2.     <!-- Context Configuration locations for Spring XML files -->  
    3.        <param-name>contextConfigLocation</param-name>  
    4.        <param-value>  
    5.        ./WEB-INF/**/Appserver-resources.xml,  
    6.        classpath:config/aer/aerContext.xml,  
    7.        classpath:org/codehaus/xfire/spring/xfire.xml,  
    8.        ./WEB-INF/**/*.spring.xml  
    9.        </param-value>  
    10.    </context-param>  


    这个方法加载配置文件的前提是已经知道配置文件在哪里,虽然可以利用“*”通配符,但灵活度有限。

  • 相关阅读:
    This counter can increment, decrement or skip ahead by an arbitrary amount
    LUT4/MUXF5/MUXF6 logic : Multiplexer 8:1
    synthesisable VHDL for a fixed ratio frequency divider
    Bucket Brigade FIFO SRL16E ( VHDL )
    srl16e fifo verilog
    DualPort Block RAM with Two Write Ports and Bytewide Write Enable in ReadFirst Mode
    Parametrilayze based on SRL16 shift register FIFO
    stm32 spi sdcard fatfs
    SPI bus master for System09 (2)
    SQLSERVER中的自旋锁
  • 原文地址:https://www.cnblogs.com/Neil223/p/5304229.html
Copyright © 2011-2022 走看看