JavaInterview JavaInterview
首页
指南
分类
标签
归档
  • CSDN (opens new window)
  • 文档集合 (opens new window)
  • 系统架构 (opens new window)
  • 微信号 (opens new window)
  • 公众号 (opens new window)

『Java面试+Java学习』
首页
指南
分类
标签
归档
  • CSDN (opens new window)
  • 文档集合 (opens new window)
  • 系统架构 (opens new window)
  • 微信号 (opens new window)
  • 公众号 (opens new window)
  • 指南
  • 简历

  • Java

  • 面试

    • 所有

    • 核心

    • 源码

    • 算法

    • 高频

      • 笔试基础题
      • MySQL各个版本区别
      • MySQL自身如何处理死锁
      • 为什么是先添加队列而不是先创建最大线程
      • 数据库常用的锁有哪些
      • 生产者和消费者5种实现方式
      • Redis内存管理的基石zmalloc
      • 如何动态修改线程池参数
      • 流量高峰时的性能瓶颈有哪些以及如何来解决
      • Java SPI与Dubbo SPI有什么区别
      • JVM运行时数据区(堆、栈、方法区)指向关系
      • Java new对象过程
      • 池化技术(线程池、连接池、内存池)
      • Spring的三级缓存解决循环依赖
      • K8S面试题大全
      • 服务治理、微服务与Service Mesh
      • SpringBoot自定义starter
        • 解答
      • 限流原理解析
      • Redis部署的三种模式(主从复制、哨兵、集群)
      • 敏感词过滤(DFA算法_Trie前缀树)
      • Java线程间通信方式
      • CPU占用过高排查
      • 线程死锁及解决方案
      • 分布式事务
      • 分布式锁
      • JVM调优
      • Synchronized锁升级(锁膨胀)
      • Paxos、Raft、ZAB协议
  • 算法

  • interview
  • hf
JavaInterview.cn
2022-10-17
目录

SpringBoot自定义starterJava

文章发布较早,内容可能过时,阅读注意甄别。

# 解答

  1. 创建一个maven项目,在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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.wisdom</groupId>
    <artifactId>spring-boot-starter-hello</artifactId>
    <version>1.0-REALEASE</version>
    <packaging>jar</packaging>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    </project>
    

spring-boot-autoconfigure此jar包中包含大量核心注解,包含条件注解等。

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

@ConfigurationProperties(prefix = "com.wisdom")
public class HelloServiceProperties {

    private String name = "wisdom";

    private String hobby = "basketball";

    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;
    }
}

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

创建配置类

public class HelloServiceConfiguration {

    private String name;

    private String hobby;

    public String getName() {
        return "name is " + name;
    }

    public String getHobby() {
        return "hobby is " + hobby;
    }

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

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

创建自动配置类:

@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloServiceConfiguration.class)
@ConditionalOnProperty(prefix = "com.wisdom", value = "enabled", matchIfMissing = true)
public class HelloServiceAutoConfiguration {

    @Autowired
    private HelloServiceProperties helloServiceProperties;

    @Bean
    @ConditionalOnMissingBean(HelloServiceConfiguration.class)
    public HelloServiceConfiguration helloServiceConfiguration() {
        HelloServiceConfiguration helloService = new HelloServiceConfiguration();
        helloService.setName(helloServiceProperties.getName());
        helloService.setHobby(helloServiceProperties.getHobby());
        return helloService;
    }
}
  • @Configuration:表明此类是一个配置类,将变为一个bean被spring进行管理。
  • @EnableConfigurationProperties:启用属性配置,将读取HelloServiceProperties里面的属性。
  • @ConditionalOnClass:当类路径下面有HelloServiceConfiguration此类时,自动配置。
  • @ConditionalOnProperty:判断指定的属性是否具备指定的值。
  • @ConditionalOnMissingBean:当容器中没有指定bean是,创建此bean。

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

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
 com.wisdom.HelloServiceAutoConfiguration

至此,自定义的spring-boot-starter-hello编写完毕,当然springboot官方建议对于非官方的starter命名方式为xxx-spring-boot-starter。执行mvn clean install将项目打成一个jar包。

新建一个springboot项目,在pom文件中添加刚刚打包的jar的坐标。

<dependency>
           <groupId>com.wisdom</groupId>
           <artifactId>spring-boot-starter-hello</artifactId>
           <version>1.0-REALEASE</version>
</dependency>

在启动类上编写访问接口

@SpringBootApplication
@RestController
public class Springboot03Application {

    @Autowired
    private HelloServiceConfiguration helloService;

    public static void main(String[] args) {
        SpringApplication.run(Springboot03Application.class, args);
    }

    @RequestMapping("/name")
    public String getName() {
        return helloService.getName();
    }

    @RequestMapping("/hobby")
    public String getHobby() {
        return helloService.getHobby();
    }
}

启动此springboot项目,依次访问 localhost:8080/name 和localhost:8080/hobby。

由于没有在application.properties中添加指定属性,所以会使用默认的属性。

接下来在application.properties添加我们自己的属性

com.wisdom.hobby=football
com.wisdom.name=messi

重启springboot项目,继续访问

localhost:8080/name 和localhost:8080/hobby。
微信 支付宝
服务治理、微服务与Service Mesh
限流原理解析

← 服务治理、微服务与Service Mesh 限流原理解析→

最近更新
01
1637. 两点之间不包含任何点的最宽垂直区域 Java
06-26
02
1636. 按照频率将数组升序排序 Java
06-26
03
1638. 统计只差一个字符的子串数目 Java
06-26
更多文章>
Theme by Vdoing | Copyright © 2019-2025 JavaInterview.cn
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式