In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
It is believed that many inexperienced people don't know what to do about how to realize multi-environment configuration in springboot. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
Catalogue
Function description
Specific configuration and details
Packing and filtering
Function description
Sometimes, a project needs to adapt to multiple development environments, such as different databases (mysql, oracle, db2, etc.), different development environments (dev, pro, test) and other different environments need to specify different configurations. In this case, we can use the configuration Profiles to control. Specify different configuration combinations at startup, and maven will automatically select the specified configuration when build.
Specific configuration and details
Configure first configure Profiles configuration in pom
Mysql mysql oracle oracle db2 db2 dev dev Prd prd
The variable properties attribute in pom can be referenced in application.yml in springboot in the way * * @ variable @ * *
Mybatis: configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl mapper-locations: [mybatis/**/**@spring.profiles.active@**/*Mapper.xml] type-aliases-package: com.*.*.domain.entity,com.*.*.system.entity
Create configuration files for different environments and distinguish them in the form of directories (of course, they can also be distinguished by different beginning names)
The following is the directory structure for multi-environment configuration
The following is the configuration of multiple databases, configured in each environment application
Configurationpublic class DatasourceConfig {@ Resource Environment env; @ Bean @ Profile (value= "mysql") public DataSource mysql () {return dataSources ("mysql");} @ Bean @ Profile (value= "oracle") public DataSource oracle () {return dataSources ("oracle");} @ Bean @ Profile (value= "db2") public DataSource db2 () {return dataSources ("db2") } / * get data source * @ param type (mysql,oracle,db2....) * / public DataSource dataSources (String type) {String driverClassName = env.getProperty ("mydatasource." + type+ ".driver-class-name"); String url = env.getProperty ("mydatasource." + type+ ".url"); String username = env.getProperty ("mydatasource." + type+ ".username") String password = env.getProperty ("mydatasource." + type+ ".password"); DruidDataSource druidDataSource = new DruidDataSource (); druidDataSource.setName (type); druidDataSource.setDriverClassName (driverClassName); druidDataSource.setUrl (url); druidDataSource.setUsername (username); druidDataSource.setPassword (password); / / TODO. Return druidDataSource;}} Packaging filtering
Resource filtering under the pom.xml build tag
Src/main/resources true/false src/main/resources config/ mybatis/ src/main/resources/config/$ {package.environment} config.properties src/main/resources mybatis/**/$ {spring.profiles.active} / * * org.apache.maven.plugins maven-jar-plugin com/yuyi/imap/ServletInitializer.class
Configure profiles multi-environment packaging and filtering
Mysql mysql jar org.springframework.boot spring-boot-starter-quartz mysql mysql-connector-java 5.1.43 org.apache.maven.plugins maven-jar-plugin Com/yuyi/imap/oracle/* com/yuyi/imap/db2/* Oracle oracle war org.apache.maven.plugins maven-war-plugin WEB-INF/classes/com/yuyi/imap/mysql/ WEB-INF/classes/com/yuyi/imap/db2/ org.apache.maven.plugins maven-jar- Plugin com/yuyi/imap/mysql/ com/yuyi/imap/db2/*
Different implementations of the same interface can be implemented through duplicate names and annotations
There are two implementations of the same interface, and the same name is used, so errors will be reported at startup (aliases are duplicated), so unused directories need to be excluded and not compiled into class. Idea is used here.
Doing so will only affect the startup of springboot, not when packaging.
By specifying profile when packaging, you can exclude resource files and java code from different environments, as well as jar, with a minimum package.
After reading the above, have you mastered how to implement multi-environment configuration in springboot? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.