如何封装springboot的starter(第一天)

--为啥要封装starter
--如何封装
--测试

为啥要封装starter

springboot的starter开箱即用,只需要引入依赖,就可以帮你自动装配bean,这样可以让开发者不需要过多的关注框架的配置。

如何封装

新建SpringBoot项目,引入以下依赖包到pom.xml

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

ps: 官方的starter命名规范为:spring-boot-starter-{name}
非官方的starter命名规范为:{name}-spring-boot-starter

编写Service类


/**
 * 字符串拼接类
 */
public class WrapService extends WrapServiceProperties{

    private String str1;

    public WrapService(String str1){
        this.str1 = str1;
    }

    public String wrap(String newWord){
        return newWord + str1;
    }
}

编写配置文件读取类

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

@ConfigurationProperties("example.config")
public class WrapServiceProperties {

    private String str1;

    public String getStr1() {
        return str1;
    }

    public void setStr1(String str1) {
        this.str1 = str1;
    }
}

这里的“example.config”指的是application.xml或application.yml中配置的属性的前缀,WrapServiceProperties类里面的属性会拼接到前缀上,即是完整的配置,例如这里的配置就是 example.config.str1 = xxx

编写自动配置类

import com.tjm.service.WrapService;
import com.tjm.service.WrapServiceProperties;
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
@ConditionalOnClass(WrapService.class)
@EnableConfigurationProperties(WrapServiceProperties.class)
public class AutoConfigure {

    @Autowired
    private WrapServiceProperties properties;

    @Bean
    @ConditionalOnMissingBean(WrapService.class)
    @ConditionalOnProperty(prefix = "example.show", value="enabled", havingValue = "true")
    WrapService starterService () {
        return new WrapService(properties.getStr1());
    }
}

对以上用到的注解解释下:

1. @ConditionalOnClass,当classpath下发现该类的情况下进行自动配置。  
2. @ConditionalOnMissingBean,当Spring Context中不存在该Bean时。  
3. @ConditionalOnProperty(prefix = "example.show",value = "enabled",havingValue = "true"),当配置文件中example.show.enabled=true时。

以下为SpringBoot中所有@Conditional注解和作用

@ConditionalOnBean:当容器中有指定的Bean的条件下  
@ConditionalOnClass:当类路径下有指定的类的条件下  
@ConditionalOnExpression:基于SpEL表达式作为判断条件  
@ConditionalOnJava:基于JVM版本作为判断条件  
@ConditionalOnJndi:在JNDI存在的条件下查找指定的位置  
@ConditionalOnMissingBean:当容器中没有指定Bean的情况下  
@ConditionalOnMissingClass:当类路径下没有指定的类的条件下  
@ConditionalOnNotWebApplication:当前项目不是Web项目的条件下  
@ConditionalOnProperty:指定的属性是否有指定的值  
@ConditionalOnResource:类路径下是否有指定的资源  
@ConditionalOnSingleCandidate:当指定的Bean在容器中只有一个,或者在有多个Bean的情况下,用来指定首选的Bean @ConditionalOnWebApplication:当前项目是Web项目的条件下  

添加spring.factories

在resource/META-INF/下面创建spring.factories文件,内容为:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.tjm.springbootstartersample.AutoConfigure

至此已经完成starter的开发,运行 mvn:install 打包。

测试

引入starter依赖

<dependency>
            <groupId>com.ltc</groupId>
            <artifactId>sample-spring-boot-starter</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <scope>test</scope>
        </dependency>

修改配置文件

在application中添加以下配置:

example.show.enabled=true
example.config.str1=youxiu

使用JUnit进行单元测试

@Autowired
	private WrapService wrapService;
	
	@Test
	public void wrapServiceTest() {
		String str = wrapService.wrap("222222222333333333");
		System.out.println(str);
	}

输出:222222222333333333youxiu

好,开发的第一个starter完成~

参考:https://www.cnblogs.com/yuansc/p/9088212.html

posted @ 2019-09-04 11:23  cilieyes  阅读(1313)  评论(0编辑  收藏  举报