We all know, online beta testing, development environment will be applied to different environment configuration files, the operating account permissions is different also, once we didn’t pay attention to when using the own environment could change the content of any other environment, will be hard to be corrected, normally we don’t need to deal with online data, so in this case, Implementation of a relatively simple way, the same JAR package in the same environment is the environment, do not need to separate packaging.

In this case, I removed the configuration file from the Resources file in Spring, leaving the resource file empty thanks to the PropertySource annotation provided by the Spring framework.

The @propertysource provides the functionality to load a specified property file. Five methods are provided in this annotation

/ / name
String name(a) default "";
// Resource file path
String[] value();
// Whether to ignore nonexistent files
boolean ignoreResourceNotFound(a) default false;
// Encoding mode
String encoding(a) default "";
// Read the factory class of the corresponding resource file
Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
Copy the code

Normally with the value in use resource files to find the attribute, the SRC provides two example Java doc: ‘the classpath: / com/myco/app. The properties’ and “file: / path/to/file. The XML”

Our architecture uses a modular management approach

 -base
 -sys
 -db
 -admin
 -pay
 -job
Copy the code

In practice we provide a class in each package directory of sys, DB, admin, base to read configuration files for example

//demo1
@Component
@PropertySource(value{"file:${spring_config}/db.properties"})
class DbPropertySource{}// demo2
@Component
@PropertySource(value{"file:${spring_config}/sys.properties"})
class SysdbPropertySource{}Copy the code

Spring_config = /usr/config; spring_config = /usr/config;

In db. Properties we only need to configure the database connection properties. The way to do this is to avoid the similar coordination between admin and SYS, which is difficult to maintain, and we can also separate the configuration properties and write some general configuration files in base, while sys only needs to write the configuration files required by SYS. Prepare the database configuration in DB, maintain the background server configuration file in admin

For example, here is part of the configuration property paste for db.properties

# mysql spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver Spring. The datasource. Url = JDBC: mysql: / / 127.0.0.1:3306 / test_db spring. The datasource. The username = root spring.datasource.password=admin spring.datasource.type=com.zaxxer.hikari.HikariDataSource spring.datasource.hikari.minimum-idle=5 spring.datasource.hikari.maximum-pool-size=15 spring.datasource.hikari.auto-commit=true spring.datasource.hikari.idle-timeout=30000 spring.datasource.hikari.pool-name=DatebookHikariCP spring.datasource.hikari.max-lifetime=1800000 spring.datasource.hikari.connection-timeout=30000 spring.datasource.hikari.connection-test-query=SELECT 1 #redis Host =127.0.0.1 spring.redis.port=9379 spring.redis.password=klfsh20190517 spring.redis.database=1 #mongo Spring. The data. The mongo. Uri = mongo: / / admin: [email protected]:3701 / test_db spring. The data. The mongo. Connections - per - host = 20 spring.data.mongodb.threads-allowed-to-block-for-connection-multiplier=10 spring.data.mongodb.connect-timeout=5000 spring.data.mongodb.socket-timeout=3000 spring.data.mongodb.max-wait-time=1500 spring.data.mongodb.auto-connect-retry=true spring.data.mongodb.socket-keep-alive=true spring.data.mongodb.prepare.maxConnectionIdleTime=60000 spring.data.mongodb.prepare.maxConnectionLifeTime=0Copy the code

This is a relatively simple way to achieve the same jar packages can be used in different environment, a solution, of course not think this is a very good solution, utilizing the means such as configuration center may better to deal with, but because of the existing enterprise size limits, do not need to use too complex scenarios to use technology to deal with, So I found a way to process the configuration files, so that I could hide some of the online configuration files, so that the developers could focus on development and testing, and also bring security to the enterprise data.