Spring Boot starter介绍以及自定义starter

1、starter介绍

简介: 可插拔插件

与jar包区别: starter能实现自动配置

作用: 大幅提高开发效率

2、常用的starter

名称 描述
spring-boot-starter-thymeleaf 使MVC Web applications 支持Thymeleaf
spring-boot-starter-mail 使用Java Mail、Spring email发送支持
spirng-boot-starter-data-redis 通过Spring Data Redis、Jedis client使用Redis键值存储数据库
spirng-boot-starter-web 构建web,包含RESTful风格架构SpringMVC和默认的切入式容器Tomcat
spirng-boot-starter-activermq 为JMS使用Apache ActiveMQ
spirng-boot-starter-data-elasticsearch  使用Elasticsearch、analytics engine、Spring Data Elasticsearch
spirng-boot-starter-aop

通过Spring AOP、AspectJ面向切面编程

spirng-boot-starter-security 使用Spring Security
spirng-boot-starter-data-jpa 通过Hibernate使用Spring Data JPA
spirng-boot-starter Core starter,包括自动配置支、logging and YAML
spirng-boot-starter-freemarker 使MVC Web Applications支持FreeMarker
spirng-boot-starter-batch 使用Spring Batch    
spirng-boot-starter-solr 通过Spring Data Solr使用Apache Solr
spirng-boot-starter-mongodb 使用MongoDB文件存储数据库、Spring Data MongoDB

3、自定义starter

1、新建工程

下一步,取名为weacher(天气小工具)

创建成功后,增加自动配置

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-autoconfigure</artifactId>
		</dependency>

  

2) 创建WeatherSource 类(属性源)

@ConfigurationProperties(prefix = "weather")
public class WeatherSource {

    private String type;

    private String rate;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getRate() {
        return rate;
    }

    public void setRate(String rate) {
        this.rate = rate;
    }
}

  

3) 创建WeatherService 服务,供外部调用

public class WeatherService {

    private  WeatherSource weatherSource;

    public WeatherService(WeatherSource weatherSource) {
        this.weatherSource = weatherSource;
    }

    public String getType(){
        return  weatherSource.getType();
    }

    public String getRate(){
        return  weatherSource.getRate();
    }
}

  

4)创建WeatherAutoConfiguration 。

增加注解EnableConfigurationProperties, ConditionalOnProperty。属性weather.enable值为enable才会自动注入

@Configuration
@EnableConfigurationProperties(WeatherSource.class)
@ConditionalOnProperty(name = "weather.enable", havingValue = "enable") //属性weather.enable值为enable才会自动注入
public class WeatherAutoConfiguration {

    @Autowired
    private  WeatherSource weatherSource;


    @Bean
    @ConditionalOnMissingBean(WeatherService.class)
    public WeatherService weatherService(){
        return  new WeatherService(weatherSource);
    }

}

  

ConditionalOnMissingBean注解下方法的作用: 当引用方没有手工定义WeatherService的时候,我们自动注入WeatherService

5) spring.factories中添加自动配置类实现

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.demo.WeatherAutoConfiguration

  

6) 安装Maven helper插件

7)将artifactId改成weather-spring-boot-starter

 生成jar文件

在sb2工程中使用这个starter

1)我们在sb2工程中引入这个jar包

2)sb2增加配置如下

3)增加接口

4) 运行结果如下

5)修改配置文件weather.enable=disable

将出现如下错误。不能找到WeacherService

原文地址:https://www.cnblogs.com/linlf03/p/12442717.html