码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • SpringBoot-自定义Starter


    目录

    一、starter启动原理

             二、自定义 Starter


    一、starter启动原理

    • starter-pom引入 autoconfigurer 包
    • autoconfigure包中配置使用 META-INF/spring.factories 中 EnableAutoConfiguration 的值,使得项目启动加载指定的自动配置类

    二、自定义 Starter

            首先在一个工程里创建两个项目,一个普通maven项目,另一个使用spring-initializer。

             修改starter的配置文件,加入下面内容:

    1. <dependency>
    2. <groupId>com.atguigugroupId>
    3. <artifactId>atguigu-hello-spring-boot-starter-autoconfigureartifactId>
    4. <version>0.0.1-SNAPSHOTversion>
    5. dependency>

            在autoconfigure包下创建spring.factories文件

    1. # Auto Configure
    2. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    3. com.atguigu.hello.auto.HelloServiceAutoConfiguration

            创建相应的包、类

     

    1. @Configuration
    2. @EnableConfigurationProperties(HelloProperties.class) //默认HelloProperties放在容器中
    3. public class HelloServiceAutoConfiguration{
    4. @ConditionalOnMissingBean(HelloService.class)
    5. @Bean
    6. public HelloService helloService(){
    7. HelloService helloService = new HelloService();
    8. return helloService;
    9. }
    10. }
    1. /**
    2. * 默认不要放在容器中
    3. */
    4. public class HelloService {
    5. @Autowired
    6. HelloProperties helloProperties;
    7. public String sayHello(String userName){
    8. return helloProperties.getPrefix() + ":"+userName+"》"+helloProperties.getSuffix();
    9. }
    10. }
    1. @ConfigurationProperties("atguigu.hello")
    2. public class HelloProperties {
    3. private String prefix;
    4. private String suffix;
    5. public String getPrefix() {
    6. return prefix;
    7. }
    8. public void setPrefix(String prefix) {
    9. this.prefix = prefix;
    10. }
    11. public String getSuffix() {
    12. return suffix;
    13. }
    14. public void setSuffix(String suffix) {
    15. this.suffix = suffix;
    16. }
    17. }

            分别clean、install autoconfigure包和starter包,存到本地maven仓库中

             去新的工程中引用自定义starter包

    1. <dependency>
    2. <groupId>com.atguigugroupId>
    3. <artifactId>atguigu-hello-spring-boot-starterartifactId>
    4. <version>1.0-SNAPSHOTversion>
    5. dependency>

           在application.yaml中配置参数

    1. atguigu:
    2. hello:
    3. prefix: hello
    4. suffix: you are beautiful

            测试

    1. @RestController
    2. public class helloController {
    3. @Autowired
    4. HelloService helloService;
    5. @GetMapping("/iKun")
    6. public String helloIKun(){
    7. return helloService.sayHello("cxk");
    8. }
    9. }
  • 相关阅读:
    【Linux】第十四章 多线程(生产者消费者模型+POSIX信号量)
    什么?你还不知道ERD Online要干什么
    openGauss学习笔记-118 openGauss 数据库管理-设置数据库审计-维护审计日志
    【类脑计算】突触可塑性模型之Hebbian学习规则和STDP
    deeplog中输出某个 event 的概率
    市场调研实业怎样使用自动化程序自动识别信息
    [附源码]计算机毕业设计JAVAjsp宾馆客房管理系统
    使用GIt小组分工完成积分商城系统-SSM
    linux进程回收
    MyBatis中的StrictMap类
  • 原文地址:https://blog.csdn.net/weixin_62427168/article/details/126181932
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号