SpringBoot_第一个starter的编写

准备工作:

1、创建两个模块

mavne:atlgq-spring-boot-starter	//starter,负责引入自动装配
spring:atlgq-spring-boot-starter-autoconfigurer //负责自动装配

2、把不需要的文件和依赖都删除

atlgq-spring-boot-starter-autoconfigurer

pom只留下starter,把主启动都删掉

1)、启动器模块

atlgq-spring-boot-starter

只负责引入自动配置模块

<?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.atlgq</groupId>
    <artifactId>atlgq-spring-boot-starter</artifactId>
    <version>1.0-SNAPSHOT</version>
    <!--starter,只需要引用这个starter就可以了-->

    <dependencies>
        <dependency>
            <!--从atlgq-spring-boot-starter-autoconfigurer的pom文件引入-->
            <groupId>com.atlgq</groupId>
            <artifactId>atlgq-spring-boot-starter-autoconfigurer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

2)、自动配置模块

atlgq-spring-boot-starter-autoconfigurer

<?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.4.0</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.atlgq</groupId>
	<artifactId>atlgq-spring-boot-starter-autoconfigurer</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>atlgq-spring-boot-starter-autoconfigurer</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<!--引入spring-boot-starter;所有的starter的基本配置-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>


	</dependencies>

</project>

3)、配置properties文件内容

package com.atlgq.atlgq.starter;

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

//声明这是一个properties类,而且要配置它的话得以atlgq.hello开头
@ConfigurationProperties(prefix = "atlgq.hello")
public class HelloProperties {

    private String prefix;
    private String suffix;

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

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public String getPrefix() {
        return prefix;
    }

    public String getSuffix() {
        return suffix;
    }
}

4)、配置service文件

package com.atlgq.atlgq.starter;

public class HelloService {

    HelloProperties helloProperties;

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

    public HelloProperties getHelloProperties() {
        return helloProperties;
    }

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

5)、配置autoConfiguration文件

package com.atlgq.atlgq.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) //所引入的Properties文件是哪一个
public class HelloAutoConfiguration {
	//引入配置文件
    @Autowired
    HelloProperties helloProperties;
	//配置进去IOC容器
    @Bean
    public HelloService helloService(){
        //装载服务
        HelloService helloService = new HelloService();
        //配置文件
        helloService.setHelloProperties(helloProperties);
        return helloService;
    }
}

然后把这个包install出去,我们就可以在maven仓库中引用了,引用的代码是

<!--引入自定义的模块-->
        <dependency>
            <groupId>com.atlgq</groupId>
            <artifactId>atlgq-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

我们可以在需要用到这个starter的地方引入

新建一个springboot项目,新建一个controller,使用这个我们自己的starter

 	@Autowired
    HelloService helloService;

    @GetMapping(value = "/hello")
    public String hello(){
        String atlgq = helloService.sayHello("atlgq");
        return atlgq;
    }

如果要配置starter里面的东西,我们可以在application.properties文件里面配置,这个atlgq.hello取决于我们编写starter里面properties文件的设置

atlgq.hello.prefix="hello"
atlgq.hello.suffix=",are you ok?"

结果展示:
在这里插入图片描述

视频教学:尚硅谷雷丰阳老师

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值