springboot创建自定义starter

springboot如何创建starter?

1、引入maven依赖

       <?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.zengqingfa</groupId>
    <artifactId>hello-springboot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.0.0.RELEASE</version>
            <optional>true</optional>
        </dependency>
    </dependencies>

</project>

2、创建properties属性类,用于读取属性。

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

//实体类映射配置信息
@ConfigurationProperties(prefix = "com.zengqingfa")
public class HelloProperties {

    private String name="zengqingfa11";

    private String hobby="coding11";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    @Override
    public String toString() {
        return "HelloProperties{" +
                "name='" + name + '\'' +
                ", hobby='" + hobby + '\'' +
                '}';
    }
}

@ConfigurationProperties配置此注解可以自动导入application.properties配置文件中的属性,前提需要指定属性前缀prefix。如果application.properties文件中未指定相应属性,便使用默认的,如上name=“zengqingfa11”,hobby=“coding11”.

3、创建service

public class HelloService {

    private HelloProperties properties;

    public HelloService() {
    }

    public HelloService(HelloProperties properties) {
        this.properties = properties;
    }

    public String sayHello() {
        System.out.println("大家好,我叫: " + properties.getName() + ", 我喜欢" + properties.getHobby());
        return "大家好,我叫: " + properties.getName() + ", 我喜欢" + properties.getHobby();
    }

}

4、自动配置类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
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)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "com.zengqingfa", value = "enabled", matchIfMissing = true)
public class HelloServiceAutoConfiguration {

    @Autowired
    private HelloProperties properties;

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

@Configuration:表明此类是一个配置类,将变为一个bean被spring进行管理。
@EnableConfigurationProperties:启用属性配置,将读取HelloServiceProperties里面的属性。
@ConditionalOnClass:当类路径下面有HelloService此类时,自动配置。
@ConditionalOnProperty:判断指定的属性是否具备指定的值。

@ConditionalOnMissingBean:当容器中没有指定bean是,创建此bean。

spring框架还提供了很多@Condition给我们用,总结:

@ConditionalOnBean(仅仅在当前上下文中存在某个对象时,才会实例化一个Bean)

@ConditionalOnClass(某个class位于类路径上,才会实例化一个Bean)

@ConditionalOnExpression(当表达式为true的时候,才会实例化一个Bean)

@ConditionalOnMissingBean(仅仅在当前上下文中不存在某个对象时,才会实例化一个Bean)

@ConditionalOnMissingClass(某个class类路径上不存在的时候,才会实例化一个Bean)

@ConditionalOnNotWebApplication(不是web应用)

6、在resources文件夹下面新建一个META-INF文件,并在下面创建spring.factories文件,将4中的配置类进行注册。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.zqf.hello.HelloServiceAutoConfiguration

7、maven install到仓库

8、创建一个测试工程引入上面工程

  <dependency>
            <groupId>com.zengqingfa</groupId>
            <artifactId>hello-springboot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

9、application.properties

com.zengqingfa.name=zengqingfa22222
com.zengqingfa.hobby=coding2222

10、创建controller

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @RequestMapping("/hello")
    public String hello() {
        return helloService.sayHello();
    }
}

11、测试
http://localhost:8080/hello
测试结果:大家好,我叫: zengqingfa22222, 我喜欢coding2222

12、注意:
SpringBoot提供的starter以spring-boot-starter-xxx的方式命名的。官方建议自定义的starter使用xxx-spring-boot-starter命名规则。以区分SpringBoot生态提供的starter。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值