Read the configuration file as needed.
Regardless of the project size, database, development environment, and test environment, such information cannot be hard-coded in the program.
Hard coding is a waste of money. Every time information changes, the project must be re-compiled, so O & M and development cannot be separated.
In addition, the configuration is scattered in the project program, so it cannot be accurately and centrally managed, slowing down the project progress.
I think this is also the reason for the emergence of the configuration file. The configuration file is in the mainstream format of properties (key-Value Pair form), xml (object, complex data structure, only you can't think of it without xml expressions).
This article is a common and simple configuration file in the properties format. Let's take a look at several different formats of reading the configuration file, pay attention to the implementation and use of the configuration file, and select another table in the design mode.
1. Spring and Apache Commons Configuration
Spring provides the default configuration file reading implementation class org. springframework. core. io. DefaultResourceLoader.
Of course, you can also implement the org. springframework. core. io. ResourceLoader interface to customize the configuration file loading implementation class.
Org. springframework. core. io. DefaultResourceLoader core method getResource:
public Resource getResource(String location) { Assert.notNull(location, "Location must not be null"); Iterator ex = this.protocolResolvers.iterator(); Resource resource; do { if(!ex.hasNext()) { if(location.startsWith("/")) { return this.getResourceByPath(location); } if(location.startsWith("classpath:")) { return new ClassPathResource(location.substring("classpath:".length()), this.getClassLoader()); } try { URL ex1 = new URL(location); return new UrlResource(ex1); } catch (MalformedURLException var5) { return this.getResourceByPath(location); } } ProtocolResolver protocolResolver = (ProtocolResolver)ex.next(); resource = protocolResolver.resolve(location, this); } while(resource == null); return resource; }
We can see that Spring supports many input parameter paths, including starting with "/" and "classpath.
If you want to use the default configuration file loading implementation provided by Spring in the project, you can write your code in this way.
ResourceLoader resourceLoader = new DefaultResourceLoader(); Resource resource = resourceLoader.getResource("log4j.properties"); Properties props = new Properties(); props.load(resource.getInputStream());
Of course, you can also introduce the Apache Commons Configuration jar internal design.
The entire jar is no more than 400 Kb. If you have enough time, you can decompile the source code.
The usage is also very simple, and the two lines of code are OK:
PropertiesConfiguration configuration = new PropertiesConfiguration("log4j.properties"); configuration.getString("log4j.appender.file");
Apache Commons Configuration loads the configuration file core implementation class org. apache. commons. Configuration. AbstractFileConfiguration Loading Method by default:
public void load(String fileName) throws ConfigurationException { try { URL e = ConfigurationUtils.locate(this.fileSystem, this.basePath, fileName); if(e == null) { throw new ConfigurationException("Cannot locate configuration source " + fileName); } else { this.load(e); } } catch (ConfigurationException var3) { throw var3; } catch (Exception var4) { throw new ConfigurationException("Unable to load the configuration file " + fileName, var4); } }
2. JDK classic handwriting
If your project does not have much Personalized Requirements for reading configuration files, if you have enough time, If you dislike third-party Jar to occupy a place in your lib directory, or if you love programming.
By taking a closer look, you will find that the underlying layers of these open-source frameworks are loaded with configuration files by java.net. URL.
A proper design mode should be adopted for the project requirements during the configuration file loading process to support some specific operations on the configuration file.
After the file is loaded, it is instantiated as a java. util. Properties object to obtain the configuration file.
Then, you can write the JDK-only method in the sample segment. The tool class is put into the project and the compiled code cannot exceed 5 kb. The core code is as follows:
URL resource = Thread.currentThread().getContextClassLoader().getResource("log4j.properties"); Properties properties = new Properties(); properties.load(resource.openStream()); properties.getProperty(key);
Because the load of java. util. Properties carries out method overloading, you can also read the configuration file without using the URL method, or write as follows:
String url = XXXXX. class. getResource (""). getPath (). replaceAll ("% 20", ""); String path = url. substring (0, url. indexOf ("classes") + filePath; // the path of the configuration file for you. InputStream inputStream = new FileInputStream (path); BufferedReader bufferedReader = new BufferedReader (new InputStreamReader (inputStream, "UTF-8"); prop. load (bufferedReader); prop. getProperty (key );
The above code is the core code of the instance. The null and exceptions are not processed. It is for reference only.
Finally, paste your own encapsulated configuration file to load the class without using any third-party jar. You can use it if necessary.