In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "SpringBoot2 profile function is what and how to customize starter", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "SpringBoot2 profile function is what and how to customize starter"!
1 Profile function 1.1 Effective rules for profiles
In order to facilitate multi-environment adaptation, SpringBoot simplifies the profile function, and the specific usage rules are as follows: ① Multiple application-xxx.yaml configuration files can be created at one time under the resources folder, corresponding to different production and testing environments, but only files named application.yaml(or suffixes.properties) will be loaded by default, so the configuration information in the configuration files of other environments will not take effect.
② If you want to switch the configuration file environment, you can configure it in the default configuration file
spring:
profiles:
active: test
③ When the configuration items of different configuration files conflict, the default configuration file configuration is used if no other environment is activated, and the activated configuration is used if the configuration of other environment is activated in the default configuration file.
④ During the run of jar package using command line, you can repackage without modifying the configuration file, and you can modify the activated environment through command line parameter configuration. First you need to package the project and open the jar package storage location
Enter the dos window and enter commands to modify the environment and run the jar package
java -jar test-profile-0.0.1-SNAPSHOT.jar --spring.profiles.active=test
⑤我们该可以使用@Profile("xxx")注解标注在类、方法或参数绑定上,表示在指定环境下才会执行该类、方法或者进行配置文件与POJO类的绑定
1.2 外部配置源
常用可以作为外部配置源的有:Java属性文件、YAML文件、环境变量、命令行参数。其中配置文件的默认扫描位置也不只单单一个,以下五个位置都能被SpringBoot默认扫到,加载顺序由高到低但是优先级相反(也就是说配置项相同的时候后面的可以覆盖前面的):(1) classpath 根路径(2) classpath 根路径下config目录(3) 项目jar包同层级(4) 项目jar包同层级的config目录(5) config目录的直接子目录
2 自定义starter
SpringBoot的starter场景启动器想必大家都不陌生,在SpringBoot开发的时候不管进行什么开发只要用到哪种技术第一都是引入它的starter场景启动器,接下来让我们根据SpringBoot中的源码自定义一个场景启动器。
第一步: 使用Spring Initializr创建一个SpringBoot项目作为autoconfiguration,构建项目目录如下:
封装自定义starter业务的HelloService
/** * @author : mereign * @date : 2022/3/12 - 20:55 * @desc : service组件,内部定义了方法 */public class HelloService { @Autowired HelloProperties helloProperties; public String sayHello(String userName) { return helloProperties.getPrefix() + ":" + userName + "》" + helloProperties.getSuffix(); }}
封装配置文件属性的HelloProperties
/** * @author : mereign * @date : 2022/3/12 - 20:57 * @desc : 配置文件的属性封装,默认自动导入容器中 */@ConfigurationProperties("com.xiaochen")public class HelloProperties { private String prefix; private String suffix; public String getPrefix() { return prefix; } public void setPrefix(String prefix) { this.prefix = prefix; } public String getSuffix() { return suffix; } public void setSuffix(String suffix) { this.suffix = suffix; }}
决定是否注册组件的自动配置类HelloServiceAutoConfiguration
/** * @author : mereign * @date : 2022/3/12 - 21:04 * @desc : 一个自动配置类,决定是否向容器中注册service组件,以及配置文件绑定 */// 表明这是一个配置类@Configuration// 配置文件绑定@EnableConfigurationProperties(HelloProperties.class)public class HelloServiceAutoConfiguration { // 如果容器中没有这个组件就是用下面的方法进行容器的helloService组件注入,如果有的话就用容器中的 @ConditionalOnMissingBean(HelloService.class) // 容器注入组件 @Bean public HelloService helloService() { HelloService helloService = new HelloService(); return helloService; }}
resources文件夹下创建MATE-INF目录下spring.factories文件,这样才能加载到指定的自动配置类
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.xiaochen.auto.HelloServiceAutoConfiguration
第二步: 创建一个maven项目作为自定义starter,只需要在它的pom文件中导入autoconfiguration的项目依赖
com.xiaochen test-autoconfigure 0.0.1-SNAPSHOT
第三步: 分别对两个项目模块在生命周期中选择clean和install,将两个模块打成jar包
第四步: 创建测试项目,目录结构如下
pom文件中导入自定义的starter
com.xiaochen test-starter 1.0-SNAPSHOT
创建一个测试使用的controller
@RestControllerpublic class HelloController { @Autowired HelloService helloService; @GetMapping("/hel") public String sayHello() { return helloService.sayHello("张三"); }}
配置测试项目的配置文件
com.xiaochen.prefix=jaka
com.xiaochen.suffix=hafd
启动测试项目访问controller的请求映射
到此,相信大家对"SpringBoot2的profile功能是什么与怎么自定义starter"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
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.