资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

springboot微服务如何自定义starter原理

这篇文章主要为大家展示了“spring boot微服务如何自定义starter原理”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“spring boot微服务如何自定义starter原理”这篇文章吧。

10年积累的网站设计制作、成都做网站经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站后付款的网站建设流程,更有辉县免费网站建设让你可以放心的选择与我们合作。

使用spring boot开发微服务后,工程的数量大大增加(一定要按照领域来切,不要一个中间件客户端包一个),让各个jar从开发和运行时自包含成了一个重要的内容之一。spring boot starter就可以用来解决该问题(没事启动时别依赖于applicationContext.getBean获取bean进行处理,依赖关系太折腾,有时候在复杂系统中解决此事比较麻烦,需要修改开源框架代码才能实现,反过来修改开源源码后,维护也是个麻烦事)。言归正传,说说自定义starter。首先请熟悉spring boot的核心理念,不然容易为了starter而starter,这种情况太多了。

创建一个maven项目,在pom文件中添加如下依赖:


    
      org.springframework.boot
      spring-boot-autoconfigure
      2.0.0.RELEASE
    
  

  
    
      
        org.springframework.boot
        spring-boot-maven-plugin
      
    
  

创建properties属性类,用于读取属性(当然可选,如果一开始没有按照spring boot autoconfig的套路来,改起来还是挺费劲的,但是一旦这么做了,就会想,TMD这才是真正的开发模式,@Value那套早该丢了)。

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

  private String name = "james";

  private String hobby = "cc";

  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。

3.创建配置类

public class HelloService {

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

4.创建自动配置类:

@Configuration
@EnableConfigurationProperties(HelloServiceProperties.class)
@ConditionalOnClass(HelloServiceConfiguration.class)
@ConditionalOnProperty(prefix = "com.xxx", value = "enabled", matchIfMissing = true)@ComponentScan({"com.xxx"}) // 如果bean比较多,一般采用这种方式
public class HelloServiceAutoConfiguration {

  @Autowired
  private HelloServiceProperties helloServiceProperties;

  @Bean // bean比较少、且顺序和逻辑有特殊要求的模块,一般采用这种方式
  @ConditionalOnMissingBean(HelloServiceConfiguration.class)
  public HelloServiceConfiguration helloServiceConfiguration() {
    HelloService helloService = new HelloService();
    helloService.setName(helloServiceProperties.getName());
    helloService.setHobby(helloServiceProperties.getHobby());
    return helloService;
  }
}

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

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xxx.HelloServiceAutoConfiguration

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

7.使用@Autowired访问接口。

@SpringBootApplication
@RestController
public class Springboot03Application {

  @Autowired
  private HelloService 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();
  }
}

相比原来要使用@Import注解导入一个@Configuration类,或者在一处集中维护ComponentScan的所有路径,使用autoconfigure starter可以让应用明显实现的更加自包含和解耦。

以上是“spring boot微服务如何自定义starter原理”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


当前题目:springboot微服务如何自定义starter原理
标题路径:http://cdkjz.cn/article/jpogch.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220