IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> 25-SpringBoot 自定义starter -> 正文阅读

[Java知识库]25-SpringBoot 自定义starter

1.starter启动原理

  • starter的pom.xml引入autoconfigure依赖

在这里插入图片描述

  • autoconfigure包中配置使用META-INF/spring.factories中EnableAutoConfiguration的值,使得项目启动加载指定的自动配置类
  • 编写自动配置类 xxxAutoConfiguration -> xxxxProperties
    @Configuration
    @Conditional
    @EnableConfigurationProperties
    @Bean
  • 引入starter — xxxAutoConfiguration — 容器中放入组件 ---- 绑定xxxProperties ---- 配置项

2.自定义starter

  • 目标:创建HelloService的自定义starter。
  • 创建两个工程,分别命名为hello-spring-boot-starter(普通Maven工程),hello-spring-boot-starter-autoconfigure(需用用到Spring Initializr创建的Maven工程)。
  • hello-spring-boot-starter无需编写什么代码,只需让该工程引入hello-spring-boot-starter-autoconfigure依赖

步骤如下

1.创建空工程

1.创建空工程, 用来作为hello-spring-boot-starter和hello-spring-boot-starter-autoconfigure的父工程
在这里插入图片描述
在这里插入图片描述
然后在该工程下创建hello-spring-boot-starter和hello-spring-boot-starter-autoconfigure这两个项目模块

2.创建hello-spring-boot-starter模块

hello-spring-boot-starter为普通Maven工程, 它是场景启动器
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3.创建hello-spring-boot-starter-autoconfigure模块

用Spring Initializr创建的Maven工程hello-spring-boot-starter-autoconfigure, 它是自动配置包
在这里插入图片描述
在这里插入图片描述
什么依赖都不用选
在这里插入图片描述
在这里插入图片描述

4.修改hello-spring-boot-starter

hello-spring-boot-starter无需编写什么代码,只需让该工程引入hello-spring-boot-starter-autoconfigure依赖:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.limi</groupId>
    <artifactId>hello-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>com.limi</groupId>
            <artifactId>hello-spring-boot-starter-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

5.修改hello-spring-boot-starter-autoconfigure

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.1</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.limi</groupId>
    <artifactId>hello-spring-boot-starter-autoconfigure</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>hello-spring-boot-starter-autoconfigure</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

</project>

创建4个文件:

  • com/limi/hello/auto/HelloServiceAutoConfiguration
  • com/limi/hello/bean/HelloProperties
  • com/limi/hello/service/HelloService
  • src/main/resources/META-INF/spring.factories

在这里插入图片描述
spring.factories

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.limi.hello.auto.HelloServiceAutoConfiguration

HelloProperties

package com.limi.hello.bean;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("hello")
public class HelloProperties {

    private String prefix;

    private boolean auto;

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public boolean isAuto() {
        return auto;
    }

    public void setAuto(boolean auto) {
        this.auto = auto;
    }
}

HelloService

package com.limi.hello.service;

import com.limi.hello.bean.HelloProperties;
import org.springframework.beans.factory.annotation.Autowired;

public class HelloService {

    @Autowired
    private HelloProperties helloProperties;

    public String sayHello(String userName){
        return helloProperties.getPrefix() + ": " + userName;
    }
}

HelloServiceAutoConfiguration

package com.limi.hello.auto;

import com.limi.hello.bean.HelloProperties;
import com.limi.hello.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(HelloProperties.class)//把HelloProperties放在容器中
@ConditionalOnProperty(
        prefix = "hello",
        name = {"auto"},
        havingValue = "true",
        //matchIfMissing = true决定了此配置不存在也可以加载 ,matchIfMissing = false则配置不存在该类不加载
        matchIfMissing = false //若hello.auto=false则修饰的类不加载
        //当该类不加载时其它注解也不会生效, HelloProperties也就不会被放入容器中
)
public class HelloServiceAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService(){
        return new HelloService();
    }
}

6.用maven插件,将两工程install到本地

在这里插入图片描述
在这里插入图片描述

3.使用starer

接下来,测试使用自定义starter, 创建web项目,并在pom.xml中引入自定义starter的依赖

        <dependency>
            <groupId>com.limi</groupId>
            <artifactId>hello-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

编写测试

package com.limi.springboottest2.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.limi.hello.service.HelloService;


@Controller
public class HelloController {

    //测试使用starter里的类
    @Autowired
    private HelloService helloService;
    
    @ResponseBody
    @GetMapping("/test")
    public String test(){
        return helloService.sayHello("andy");
    }
}

1.当hello.auto=false

application.properties

server.port=8080
hello.prefix=How are you?
hello.auto=false

报错找不到bean, 即组件没有注册
在这里插入图片描述

2.当hello.auto=true

application.properties

server.port=8080
hello.prefix=How are you?
hello.auto=false

组件注册成功,项目正常运行
在这里插入图片描述

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-07-03 10:36:15  更:2022-07-03 10:37:38 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 20:42:16-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码