SpringBootStarter

简单的SpringBootStarter

新建项目
 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.1.13.RELEASE</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.13.RELEASE</version>
        </dependency>
        <!--springBoot-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.1.13.RELEASE</version>
        </dependency>
        <!-- Lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
    </dependencies>

创建配置类
@ConfigurationProperties("zhangfei.hello")
public class HelloProperties {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
创建对外提供的接口
public class MyStartController {
    HelloProperties helloProperties;
    public MyStartController(HelloProperties helloProperties) {
        this.helloProperties=helloProperties;
    }
    public String index(){
        return helloProperties.getName()+"欢迎您";
    }
}
启动类
  • 启动类
  • 首先看引用项目的配置 是否有zhangfei.hello.name 这个属性
  • 有的话 加载HelloProperties.class 然后开始注入bean信息(MyStartController )
  • 外部引入jar包即可用
@Configuration
@ConditionalOnProperty(value = "zhangfei.hello.name")
@EnableConfigurationProperties(HelloProperties.class)
public class HelloAutoConfitguration {

    @Autowired
    HelloProperties helloProperties;

    @Bean
    public MyStartController indexController(){
        return new MyStartController(helloProperties);
    }
}
启动类向导
  • 这个一定注意
    • resources下创建META-INF
    • 在META-INF下创建文件spring.factories 而不是在创建再创建spring文件夹然后再创建
    • 如果factories文件
    • 如果META-INF下面创建spring的话 再spring文件夹创建org.springframework.boot.autoconfigure.AutoConfiguration.imports是可以的
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.gouying.web.config.HelloAutoConfitguration
自定义Spring Boot Starter 可以让我们将一些常用的功能封装成一个 Starter,方便其他项目引入。 下面是一个自定义 Starter 的示例: 1. 首先,创建一个 Maven 项目,并添加 Spring Boot Starter Parent 依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.0</version> <type>pom</type> </dependency> ``` 2. 创建一个自定义 Starter 模块,并添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> ``` 3. 在 src/main/resources 目录下创建一个 spring.factories 文件,并添加以下内容: ```properties org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.myproject.MyAutoConfiguration ``` 4. 创建一个自动配置类 MyAutoConfiguration,添加以下代码: ```java @Configuration @ConditionalOnClass(MyService.class) @EnableConfigurationProperties(MyProperties.class) public class MyAutoConfiguration { @Autowired private MyProperties properties; @Bean @ConditionalOnMissingBean public MyService myService() { MyService myService = new MyService(); myService.setMessage(properties.getMessage()); return myService; } } ``` 5. 创建一个配置类 MyProperties,添加以下代码: ```java @ConfigurationProperties(prefix = "my") public class MyProperties { private String message = "Hello World"; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } } ``` 6. 创建一个服务类 MyService,添加以下代码: ```java public class MyService { private String message; public void setMessage(String message) { this.message = message; } public String getMessage() { return message; } } ``` 7. 最后,在其他项目中,添加以下依赖即可引入自定义 Starter: ```xml <dependency> <groupId>com.example</groupId> <artifactId>my-spring-boot-starter</artifactId> <version>1.0.0-SNAPSHOT</version> </dependency> ``` 以上就是自定义 Spring Boot Starter 的基本流程,大家可以根据自己的实际需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值