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-starterCore 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、新建工程

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

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.alen</groupId>
    <artifactId>spring-boot-starter-price</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!--
                springboot的spring-boot-autoconfigure模块通过灵活的Auto-configuration注解
                使SpringBoot中的功能实现模块化和可被替换扩展。
                spring-boot-autoconfigure思路类似SPI(Service Provider Interface),
                都是不同的实现类实现了定义的接口
        -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <!--导入配置文件处理器,配置文件进行绑定就会有提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>

    </dependencies>


    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

  

2) 创建PriceSource类(属性源)

@ConfigurationProperties(prefix = "price")
public class PriceSource {

    private  String type;
     private  String msg;

    public String getType() {
        return type;
    }

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

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

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

public class PriceService {
    private PriceSource priceSource;

    public PriceService(PriceSource priceSource) {
        this.priceSource = priceSource;
    }

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

    public String getMsg() {
        return priceSource.getMsg();
    }

}

 

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

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

    @Autowired
    private  PriceSource priceSource;

    @Bean
    @ConditionalOnMissingBean(PriceService.class)
    public PriceService priceService(){
        return  new PriceService(priceSource);
    }
}

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

 

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

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.alen.redission.config.PriceAutoConfiguration

 

在另一个工程中使用这个starter

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

 <dependencies>
        <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->
        <!-- spring mvc (springboot 默认集成) springboot-web依赖即可 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 去掉springboot默认配置 -->
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.alen</groupId>
            <artifactId>spring-boot-starter-price</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

2)application.yml增加配置如下

price:
  enable: enable
  type: order
  msg: 订单金额

 

3)测试类

@SpringBootTest
class SpringBootSourceApplicationTests {

    @Autowired
    private PriceService priceService;

    @Test
    void contextLoads() {
        System.out.println("---Type--" + priceService.getType() + "---Msg----" + priceService.getMsg());
    }
}

4) 运行结果如下

---Type--order---Msg----订单金额

 

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

会报错

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值