Spring loading configuration file path method

BeanFactory has many implementation classes, usually the org.springframework.beans.factory.xml.XmlBeanFactory class is used. But for most J2EE applications, ApplicationContext is recommended. ApplicationContext is

a sub-interface of BeanFactory, and its commonly used implementation classes are org.springframework.context.support.FileSystemXmlApplicationContext and org.springframework.context.support.ClassXmlAplicationContext.
The configuration information of Springr is usually set using XML configuration files. Therefore, when creating a BeanFactory instance, the XML configuration file should be provided as a parameter. ,

The actual application of ApplicationContext is described in detail below:



1: ClassPathXmlApplicationContext
1. No prefix: The default is the relative path under the project's classpath
   ApplicationContext appCt = new ClassPathXmlApplicationContext("app.spring.xml");

2. The prefix classpath: indicates the project's The relative path
   ApplicationContext under the classpath appCt = new ClassPathXmlApplicationContext("classpath:app.spring.xml");

3. Use the prefix file to indicate the absolute path of the file
   ApplicationContext appCt = new ClassPathXmlApplicationContext("file:D:/app.spring.xml");

4. Multiple files can be loaded at the same time
  String[] xmlCfg = new String[] { "classpath:base.spring.xml","app .spring.xml   " }; ApplicationContext appCt
  = new ClassPathXmlApplicationContext(xmlCfg); 5.

Use wildcards to load all files that meet the requirements The path is the root directory of the project ApplicationContext appCt2 = new FileSystemXmlApplicationContext("src/main/resources/app.spring.xml"); 2. The prefix classpath: indicates the relative path under the classpath of the project    ApplicationContext appCt2 = new FileSystemXmlApplicationContext("classpath: app.spring.xml"); 3. Use the prefix file to indicate the absolute path of the file










   ApplicationContext appCt2 = new FileSystemXmlApplicationContext("file:D:/app.spring.xml");
   ApplicationContext appCt2 = new FileSystemXmlApplicationContext("D:/app.spring.xml");

4. Multiple files can be loaded at the same time
  String[] xmlCfg = new String[] { "src/main/resources/base.spring.xml","classpath:app.spring.xml"};
  ApplicationContext appCt2 = new FileSystemXmlApplicationContext(xmlCfg);

5. Use wildcards to load all files that meet the requirements
  ApplicationContext appCt2 = new FileSystemXmlApplicationContext("classpath:*.spring.xml");


6.Resource

ResourcePatternResolver resolver=new PathmatchingResourceResolver();
Resource res = resolver.getResource("classpath:com/hh/beans.xml");
Beanfactory    bf = new  XmlBeanFactory(res);
Car  car= bf.getBean("car",Car.class)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326784786&siteId=291194637