SpringBoot2.2.x(十二)自定义starter

常用注解

@Configuration
指定这个类是一个配置类

@ConditionalOnClass
当类路径classpath下有指定的类的情况下进行自动配置

@ConditionalOnMissingClass

当类路径下没有指定的类的条件下

@ConditionalOnBean

当容器(Spring Context)中有指定的Bean的条件下

@ConditionalOnMissingBean

当容器(Spring Context)中没有指定Bean的情况下进行自动配置

@ConfigurationProperties
结合相关XxxProperties来绑定相关配置, 主要用来把properties配置文件转化为对应的XxxProperties来使用的, 并不会把该类放入到IOC容器中

@EnableConfigurationProperties(XxxProperties.class)

注解的作用是使@ConfigurationProperties注解生效并且将XxxProperties注入到IOC容器中。

如果在每个Properties上都使用@Component来标注,那么也不需要使用@EnableConfigurationProperties({XxxProperties.class})来标注了,同样也可以在Spring上下文容器中也能获取到XxxProperties对应的Bean。

@ConditionalOnProperty(prefix = “spring.person”, value = “enabled”, matchIfMissing = true)
当配置文件中spring.person.enabled=true时进行自动配置,如果没有设置此值就默认使用matchIfMissing对应的值。如果不设置matchIfMissing = true, 默认为false。

META-INF/spring.factories

自动配置能加载,需要将自动配置类放在META-INF/spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
xxx.xxx.xxxAutoConfiguration,\
aaa.aaa.aaaAutoConfiguration

编写starter

pom依赖

<?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.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example.springboot</groupId>
    <artifactId>lzc-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>lzc-spring-boot-starter</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
</project>

PersonProperties.java

@ConfigurationProperties(prefix = "spring.person")
public class PersonProperties {
    // 姓名
    private String name;
    // 年龄
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

PersonService.java

public class PersonService {
    private PersonProperties properties;
    public PersonService() {

    }
    public PersonService(PersonProperties properties) {
        this.properties = properties;
    }
    public void sayHello(){
        System.out.println("name = " + properties.getName() + ", age = " + properties.getAge());
    }
}

PersonServiceAutoConfiguration.java

@Configuration
@EnableConfigurationProperties(PersonProperties.class)
@ConditionalOnClass(PersonService.class)
@ConditionalOnProperty(prefix = "spring.person", value = "enabled", matchIfMissing = true)
public class PersonServiceAutoConfiguration {
    @Autowired
    private PersonProperties properties;
    @Bean
    @ConditionalOnMissingBean(PersonService.class)  // 当容器中没有指定Bean的情况下,自动配置PersonService类
    public PersonService personService(){
        PersonService personService = new PersonService(properties);
        return personService;
    }
}

最后使用maven命令:mvn install, 打包到本地仓库

在其它工程中引入依赖包

<dependency>
    <groupId>com.example.springboot</groupId>
    <artifactId>lzc-spring-boot-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值