It is very convenient to read custom configuration files in spring boot, and it is not necessary to write the read class of property to read the configuration file information.
Here are a few ways I've tried to read springboot's read configuration files:
Prepare:
1. Add configuration information to the application.yml file:
hank: testConfig: TestDriver
2. Create a new configuration file dbConfig.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test jdbc.username=root jdbc.password=root org.hank.testconfig = testConfig
We know in the previous preparation
hank.testConfig is in the application.yml file, which is springboot default configuration file jdbc.driver is the key in our custom configuration file
Method 1: Use the class Environment that comes with springboot
test:
@Autowired private Environment env; @Test public void testConfigDefault() { logger.info("Environment get default properties:" + env.getProperty("hank.testConfig")); logger.info("Environment get self properties:" + env.getProperty("jdbc.driver")); }
Test Results
Environment get default properties:TestDriver
Environment get self properties:null
Conclusion: That is to say, the environment that comes with the environment can only read the configuration information in the default configuration file, and the customized configuration file cannot be read in the environment. Moreover, the items configured in application.yml are the items of the system itself, that is, the configuration items that do not change with the change of the system environment. For some volatile configuration items, it is better for us to customize the file. I don't usually configure it this way.
Method 2: Configurable
Create class ConfigDefault annotation @Configurable
@Component @Configurable public class ConfigDefault { @Value("${hank.testConfig}") private String hankConfig; @Value("${org.hank.testconfig}") private String selfConfig; getter and setter.... }
test:
@Autowired private ConfigDefault configDefault; @Test public void testConfigDefault() { logger.info("defualt config--hank.testConfig:" + configDefault.getHankConfig()); logger.info("self config--org.hank.testconfig:" + configDefault.getSelfConfig()); }
Test result: report an error directly, Could not resolve placeholder 'org.hank.testconfig' in value "${org.hank.testconfig}"
That is to say, there is no org.hank.testconfig configuration item in the application.yml file, so adding @Configurable to the class is also a configuration item that only reads the application.yml file by default.
Method 3: @PropertySource annotation
Create a new model class:
@Component @PropertySource("classpath:dbConfig.properties") public class DataBaseConfig { @Value("${jdbc.driver}") private String driver; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String userName; @Value("${jdbc.password}") private String password; @Value("${hank.testConfig}") //Test the default configuration file private String hankConfig; getter and setter... }
test:
@Autowired private DataBaseConfig config; @Test public void testGetDataBaseConfig() { logger.info("self Config Driver:" + config.getDriver()); logger.info("default config hank.testConfig:" + config.getHankConfig()); }
Test Results:
self Config Driver:com.mysql.jdbc.Driver
default config hank.testConfig:TestDriver
It can be seen that the information along with the default configuration is also read
Conclusion: use @PropertySource("classpath:dbConfig.properties")
Specifying the custom configuration file path can read the custom configuration file information, and for the default configuration file application.yml, we can also read the information of the default configuration file while reading the custom configuration file.
The above is the way to read the configuration file in the three. You can see that if you want to read a custom configuration file, you must annotate the specified configuration file path.