Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to load external configuration data in Springboot

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to load external configuration data in Springboot, which is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

The configuration file format is as follows:

{"id": "1", "organization": "XX people's Hospital", "api_server": "http://192.168.1.120:8010/data-sync-server/upload"," task_repo ":", "runtime_repo": "", "mysql_uri": "jdbc:mysql://192.168.1.11:3306/engine?user=root&password=bhgx_yf2&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8" "mongodb_uri": "mongodb://test:test123@192.168.1.18:38000/test", "engine_uri": "http://192.168.1.120:8008/engine/task"}"

What we want to load is mysql_url, and the code is as follows:

Import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.boot.jdbc.DataSourceBuilder;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.sql.DataSource;/** * external configuration file loading data source * / @ Configurationpublic class DataSourceConfig {private static Logger logger = LoggerFactory.getLogger (DataSourceConfig.class) @ Bean public DataSource getDataSource () {/ / get all-in-one configuration information GlobalUtils.getGxaioInfo (Constant.GXAIO_HOME + Constant.GXAIO_AIOCNF_FILE); DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create (); dataSourceBuilder.driverClassName ("com.mysql.cj.jdbc.Driver"); dataSourceBuilder.url (Constant.GXAIO_CONFIG_MAP.get ("mysql_uri")); return dataSourceBuilder.build ();}}

In this way, the configuration of mysql in the application.properties configuration file can be deleted

By the way, put the external configuration information of mongodb.

Import com.mongodb.MongoClientURI;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.mongodb.MongoDbFactory;import org.springframework.data.mongodb.core.SimpleMongoDbFactory / * external configuration file loading data source * / @ Configurationpublic class MongoDataSourceConfig {@ Bean public MongoDbFactory mongoDbFactory () {/ / get the all-in-one configuration information in the environment variables GlobalUtils.getGxaioInfo (Constant.GXAIO_HOME + Constant.GXAIO_AIOCNF_FILE); MongoClientURI uri = new MongoClientURI (Constant.GXAIO_CONFIG_MAP.get ("mongodb")); / / create MongoDbFactory MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory (uri) Return mongoDbFactory;}} the above is how to load external configuration data in Springboot. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report