Getting Started 14 - Available in resource files
Let's take a look at the actual application of the ApplicationContext interface. We used ClassPathXMLApplicationContext, which reads the resource file acquired in the ClassPath root directory (plus the specified path). We can also use FileSystemXmllapplicationContext, which uses files with respect to the root directory to get resource files, such as if your work directory is "c: / Workspace / SpringTest /", and you specify "bean.xml in the program" Bean.xml ", The program will go to your" C: / Workspace / SpringTest / Bean.xml "to find the resource file you specified, as you specify" conf / somepro.properties ", will come to C: / Workspace Profile you specify in /SPRINGTEST/CONF/SOMEPRO.PROPERTIES. For web applications, you can use XMLWebApplicationContext, which will use your web application as the starting point of the relative path, for example, if you specify "Web-INF / CONF / Admin.properties", your web application is SpringWeb, You will find your profile to "SpringWeb / Web-INF / CONF / Admin.properties". Spring provides generic access for resource files, which is a good application to access resource files, the most important feature, in addition to the above scheme, ApplicationContext inherits the ResourceLoader interface, you You can use the getResource () method and specify the URL of the resource file to get an instance of a practical resource interface, for example:
Resource resource = context.getResource ("ClassPath: admin.properties);
"ClassPath:" is the URL virtual agreement for Spring Custom, which will retrieve an org.springframework.core.io.classpathResource instance, the resource file is located in the ClassPath root directory, you can also specify the standard URL, like "File" "
Or "http:", for example:
Resource Resource = Context.getResource ("File: C: /Workspace/springtest/conf/admin.properties);
This will return an org.springframework.core.io.FileSystemResource instance, or you can specify as follows to return a servletContextResource instance:
Resource resource = context.getResource ("Web-INF / Conf / Admin.properties);
After obtaining an instance of the actual resource interface, you can use getFile (), getInputStream () and other ways or obtain the resource of the resource file, the instance of the resource interface is just an abstract representative of the resource file, refers to the actual resource file. There may not be existed, you can use the exists () method for testing.