spring boot自定义starter

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

  1. 创建一个maven项目,在pom文件中添加如下依赖:
   <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>
  1. 创建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;
    }
}
  1. 创建自动配置类:
@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;
    }
}
  1. 在resources文件夹下面新建一个META-INF文件,并在下面创建spring.factories文件,将4中的配置类进行注册。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.xxx.HelloServiceAutoConfiguration
  1. 新建一个springboot项目,在pom文件中添加刚刚打包的jar的坐标。
  2. 使用@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可以让应用明显实现的更加自包含和解耦。

原文地址:https://www.cnblogs.com/zhjh256/p/12045636.html

时间: 2024-08-29 15:39:52

spring boot自定义starter的相关文章

Spring boot 自定义starter

以下配置来自尚硅谷.. 常用如何配置 @Configuration //指定这个类是一个配置类 @ConditionalOnXXX //在指定条件成立的情况下自动配置类生效 @AutoConfigureAfter //指定自动配置类的顺序 @Bean //给容器中添加组件 @ConfigurationPropertie结合相关xxxProperties类来绑定相关的配置 @EnableConfigurationProperties //让xxxProperties生效加入到容器中 自动配置类要能

Spring Boot自定义Redis缓存配置,保存value格式JSON字符串

Spring Boot自定义Redis缓存,保存格式JSON字符串 部分内容转自 https://blog.csdn.net/caojidasabi/article/details/83059642 package springboot01cache.config; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; im

Spring Boot自定义配置实现IDE自动提示

一.背景 官方提供的spring boot starter的配置项,我们用IDE配置的时候一般都有自动提示的,如下图所示 而我们自己自定义的配置却没有,对开发非常不友好容易打错配置,那这个是怎样实现的呢? ? 二.提示原理 IDE是通过读取配置信息的元数据而实现自动提示的,而元数据在目录META-INF中的spring-configuration-metadata.json 或者 additional-spring-configuration-metadata.json ? 三.实现自动提示 以

Spring Boot自定义错误页面,Whitelabel Error Page处理方式

我已经是Spring Framework框架的忠实粉丝.对于企业软件开发者来说它提供了对常见问题的通用解决方案,包括那些你在未来开发中没有意识到的问题.但是,它构建的J2EE项目变得比较臃肿,需要被一种新的解决方案替代. 我最大的抱怨就是最开始使用spring Framework构建项目十分缓慢和复杂,比如构建一个包含JPA的MVC应用.为改变这种情况,Spring Boot应运而生了. Spring Boot以一种新的微服务的方式来替代以Spring Framework构建项目的传统方式,我已

【第四十章】Spring Boot 自定义拦截器

1.首先编写拦截器代码 package com.sarnath.interceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Service; import org.springframework.web.servlet.HandlerInterceptor; import

Spring Boot中Starter是什么

比如我们要在Spring Boot中引入Web MVC的支持时,我们通常会引入这个模块spring-boot-starter-web,而这个模块如果解压包出来会发现里面什么都没有,只定义了一些POM依赖. 经过研究,Starter主要用来简化依赖用的.比如我们之前做MVC时要引入日志组件,那么需要去找到log4j的版本,然后引入,现在有了Starter之后,直接用这个之后,log4j就自动引入了,也不用关心版本这些问题. 部分starters的依赖: Starter(Group ID: org.

spring boot 自定义Validator注解

spring boot在Post接受一个对象参数的时候可以使用@Valid去验证,如下代码 在创建一个User类 上面的@NotBlank和@Past就做了一个判断,如何做类似的注解用来验证数据 创建一个MyConstraint的接口 @Retention :用来说明该注解类的生命周期.它有以下三个参数:RetentionPolicy.SOURCE : 注解只保留在源文件中RetentionPolicy.CLASS : 注解保留在class文件中,在加载到JVM虚拟机时丢弃RetentionPo

spring boot 自定义过滤器链

spring boot 会按照order值的大小,从大到小的顺序来依次过滤. 贴下代码: package com.osp.ucenter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootAp

spring security +spring boot 自定义 403 页面

用的spring security  做的权限控制, 当  访问没有权限, 跳转 会跳到默认403 页面.不符合当前项目需求. 一下是解决方式: package com.ycmedia; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.au