SpringBoot学习笔记-8:第八章 Spring Boot 自定义 starters

简介: SpringBoot学习笔记-8:第八章 Spring Boot 自定义 starters

第八章 Spring Boot 自定义 starters

自动配置类

@Configuration   // 指定这个类是配置类
@Conditionalxxx  // 指定条件成立的情况下自动配置类生效
@AutoConfigureAfter // 指定自动配置类的顺序
@Bean // 给容器中添加组件
@ConfigurationProperties  // 结合相关Properties类来绑定相关的配置
@EnableConfigurationProperties // 让Properties生效加入到容器中

启动器 Starter


启动器模块是一个空的 JAR 文件,仅提供辅助性依赖管理,这些依赖可能用于自动装配或者其他类库


命名规约:

推荐使用一下命名规约


官方命名空间:

前缀:spring-boot-starter-

模式:spring-boot-starter-模块名

举例:spring-boot-starter-web、spring-boot-starter-jdbc


自定义命名空间:

前缀:-spring-boot-starter

模式:模块名-spring-boot-starter

举例:mybatis-spring-boot-starter


自定义 starter

1、Idea 创建空工程 Empty Project

2、新建 spring 自动配置模块

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.3.1.RELEASE</version>
        <relativePath/>
        <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mouday</groupId>
    <artifactId>mouday-spring-boot-starter-autoconfigurer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mouday-spring-boot-starter-autoconfigurer</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <!--所有starter的基本配置-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
</project>

属性文件 HelloProperties.java

package com.mouday.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "mouday.hello")
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;
    }
}

HelloService.java

package com.mouday.starter;
public class HelloService {
    HelloProperties properties;
    public String  sayHello(String name){
        return properties.getPrefix() + name + properties.getSuffix();
    }
    public HelloProperties getProperties() {
        return properties;
    }
    public void setProperties(HelloProperties properties) {
        this.properties = properties;
    }
}

配置类

package com.mouday.starter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnWebApplication // web应用才生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
    @Autowired
    HelloProperties helloProperties;
    @Bean
    public HelloService helloService(){
        HelloService service = new HelloService();
        service.setProperties(helloProperties);
        return service;
    }
}

src/main/resources/META-INF/spring.factories


org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

com.mouday.starter.HelloServiceAutoConfiguration


3、新建 maven starter 模块

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.mouday</groupId>
    <artifactId>mouday-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>com.mouday</groupId>
            <artifactId>mouday-spring-boot-starter-autoconfigurer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

测试

引入依赖 pom.xml

<dependency>
    <groupId>com.mouday</groupId>
    <artifactId>mouday-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

修改配置文件 application.properties

mouday.hello.prefix=hello
mouday.hello.suffix=hi

新建测试 Controller

package com.example.demo.controller;
import com.mouday.starter.HelloService;
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;
@Controller
public class HelloController {
    @Autowired
    HelloService helloService;
    @GetMapping("/hello")
    @ResponseBody
    public String hello(String name){
        return helloService.sayHello(name);
    }
}

http://localhost:8080/hello?name=Tom

访问结果

helloTomhi
相关文章
|
9月前
|
前端开发 Java 应用服务中间件
《深入理解Spring》 Spring Boot——约定优于配置的革命者
Spring Boot基于“约定优于配置”理念,通过自动配置、起步依赖、嵌入式容器和Actuator四大特性,简化Spring应用的开发与部署,提升效率,降低门槛,成为现代Java开发的事实标准。
|
9月前
|
前端开发 Java 微服务
《深入理解Spring》:Spring、Spring MVC与Spring Boot的深度解析
Spring Framework是Java生态的基石,提供IoC、AOP等核心功能;Spring MVC基于其构建,实现Web层MVC架构;Spring Boot则通过自动配置和内嵌服务器,极大简化了开发与部署。三者层层演进,Spring Boot并非替代,而是对前者的高效封装与增强,适用于微服务与快速开发,而深入理解Spring Framework有助于更好驾驭整体技术栈。
|
9月前
|
搜索推荐 JavaScript Java
基于springboot的儿童家长教育能力提升学习系统
本系统聚焦儿童家长教育能力提升,针对家庭教育中理念混乱、时间不足、个性化服务缺失等问题,构建科学、系统、个性化的在线学习平台。融合Spring Boot、Vue等先进技术,整合优质教育资源,提供高效便捷的学习路径,助力家长掌握科学育儿方法,促进儿童全面健康发展,推动家庭和谐与社会进步。
|
9月前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
768 2
|
10月前
|
人工智能 Java 机器人
基于Spring AI Alibaba + Spring Boot + Ollama搭建本地AI对话机器人API
Spring AI Alibaba集成Ollama,基于Java构建本地大模型应用,支持流式对话、knife4j接口可视化,实现高隐私、免API密钥的离线AI服务。
7237 2
基于Spring AI Alibaba + Spring Boot + Ollama搭建本地AI对话机器人API
存储 JSON Java
955 0
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的云的学习笔记系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的云的学习笔记系统附带文章源码部署视频讲解等
249 0
SpringBoot学习笔记-15:第十五章-SpringBoot 与部署
SpringBoot学习笔记-15:第十五章-SpringBoot 与部署
217 0
|
安全 Java 应用服务中间件
SpringBoot学习笔记(二、部署方式)
SpringBoot学习笔记(二、部署方式)
467 0
SpringBoot学习笔记(二、部署方式)
|
9月前
|
JavaScript 安全 Java
基于springboot的大学生兼职系统
本课题针对大学生兼职信息不对称、权益难保障等问题,研究基于Spring Boot、Vue、MySQL等技术的兼职系统,旨在构建安全、高效、功能完善的平台,提升大学生就业竞争力与兼职质量。

热门文章

最新文章