点击查看:LearnSpringBoot08starter
点击查看:LearnSpringBoot08starterTest
点击查看更多的SpringBoot教程

一、主要流程

1. 先创建空的project
在这里插入图片描述

2. 打开空的project 结构 图选中model 点击+
在这里插入图片描述
在这里插入图片描述

3. 创建 model(Maven)启动器
如果左边栏目有empty选项目,选中 empty
提醒:创建启动器 model在旧版本intelliJ IDEA 左边有 Maven 选项,应该选择 Maven,新版本里没有了,所以选择Spring initializr, 等创建完手动修改pom.xml文件

4.创建自动配置model
在这里插入图片描述

二、mystarter-spring-boot-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>org.example.mystarter</groupId>
    <artifactId>mystarter-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>20</maven.compiler.source>
        <maven.compiler.target>20</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

<!--    启动器-->
    <dependencies>
<!--        引入自动配置-->
        <dependency>
            <groupId>com.example.mystarter</groupId>
            <artifactId>mystarter-spring-boot-starter-autoconfigurer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

</project>

三、mystarter-spring-boot-starter-autoconfigurer模块里的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>3.1.1</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example.mystarter</groupId>
	<artifactId>mystarter-spring-boot-starter-autoconfigurer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>mystarter-spring-boot-starter-autoconfigurer</name>
	<description>mystarter-spring-boot-starter-autoconfigurer</description>
	<properties>
		<java.version>17</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

<!--		https://docs.spring.io/spring-boot/docs/3.1.1/reference/html/configuration-metadata.html#appendix.configuration-metadata.annotation-processor
	https://blog.csdn.net/Zhangsama1/article/details/129198456
	使用spring-boot-configuration-processor,作用就是将自己的配置自己创建的配置类生成元数据信息,这样就能在自己的配置文件中显示出来非常的方便
	例如:在application.yml中自定义配置信息,使用开发工具做自定义信息提示
-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>

</project>

四、LearnSpringBoot08starter工程结构图

在这里插入图片描述

五、mystarter-spring-boot-starter-autoconfigurer核心代码

HelloProperties.java代码

package com.example.mystarter;

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

@SuppressWarnings("ConfigurationProperties")
@ConfigurationProperties(prefix = "test.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.example.mystarter;

public class HelloService {

    HelloProperties helloProperties;
    public String sayHello(String name){
        return helloProperties.getPrefix() + "-" + name + helloProperties.getSuffix();
    }

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

    public void setHelloProperties(HelloProperties helloProperties) {
        this.helloProperties = helloProperties;
    }
}

HelloServiceAutoConfiguration.java代码

package com.example.mystarter;

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;

/*
https://blog.csdn.net/Zhangsama1/article/details/129198456
在SpringBoot2.7.x版本之后,慢慢不支持META-INF/spring.factories文件了,需要导入的自动配置类可以放在/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports文件中
 */
@Configuration
@ConditionalOnWebApplication// 在web应用上生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
    @Autowired
    HelloProperties helloProperties;

    @Bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}

org.springframework.boot.autoconfigure.AutoConfiguration.imports代码

com.example.mystarter.HelloServiceAutoConfiguration

在这里插入图片描述

六、将自动配置model和启动器model安装到Maven仓库

在这里插入图片描述

7、创建新的工程测试自定义starter

LearnSpringBoot08starterTest工程结构图

在这里插入图片描述

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>3.1.1</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>LearnSpringBoot08starterTest</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>LearnSpringBoot08starterTest</name>
	<description>LearnSpringBoot08starterTest</description>
	<properties>
		<java.version>17</java.version>
	</properties>
	<dependencies>

		<!--	引入自定义的启动器	-->
		<dependency>
			<groupId>org.example.mystarter</groupId>
			<artifactId>mystarter-spring-boot-starter</artifactId>
			<version>1.0-SNAPSHOT</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

HelloController.java代码

package com.example.learnspringboot08startertest.controller;

import com.example.mystarter.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    HelloService helloService;
    @GetMapping("/hello")
    public String hell(){
        return helloService.sayHello("test01");
    }
}

application.properties

server.port=8086
test.hello.prefix=CUSTOM STARTER
test.hello.suffix=HELLO WORD


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

八、测试结果

启动LearnSpringBoot08starterTest工程,在浏览器地址栏访问:http://localhost:8086/hello
在这里插入图片描述

欢迎关注我的公众号,不定期推送优质的文章,
微信扫一扫下方二维码即可关注。
在这里插入图片描述

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐