面试题- SpringBoot自定义starter

1 新建module hello-spring-boot-starter

继承 spring-boot-starter-parent



    4.0.0

    com.cdchen
    hello-spring-boot-starter
    1.0-SNAPSHOT

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.6.RELEASE
        
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
    

2 创建读取配置POJO,HelloServiceProperteis 指定前缀


import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix="cdchen")
public class HelloServiceProperteis {
    private String msg;

    public String getMsg() {
        return msg;
    }

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

3 创建测试服务类 HelloService

public class HelloService {
    private String msg;

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

    public String getMsg() {
        return msg;
    }
    public void sayHello(){
        System.out.println("hello starter: hello "+ msg);
    }
}

4 重中之重,创建自动配置类,HelloAutoConfiguration

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(value = HelloServiceProperteis.class)
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "cdchen", value = "enable", matchIfMissing = true)
public class HelloAutoConfiguration {
    @Autowired
    private HelloServiceProperteis helloServiceProperteis;

    @Bean
    @ConditionalOnMissingBean(HelloService.class)
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        helloService.setMsg(helloServiceProperteis.getMsg());
        return helloService;
    }
}

@Configuration:标识此类为一个spring配置类

@EnableConfigurationProperties(value = HelloServiceProperteis.class):启动配置文件,value用来指定我们要启用的配置类,可以有多个,多个时我们可以这么写value={xxProperties1.class,xxProperteis2.class…}

@ConditionalOnClass(HelloService.class):表示当classPath下存在HelloService.class文件时改配置文件类才有效

@ConditionalOnProperty(prefix = “cdchen”, value = “enable”, matchIfMissing = true):表示只有我们的配置文件是否配置了以hello为前缀的资源项值,并且在该资源项值为enable,如果没有配置我们默认设置为enable
如果 enable设置false 则出现下面情况
面试题- SpringBoot自定义starter_第1张图片

5 指定引导类 在resources文件夹下新建 META-INF文件夹,并在该文件夹下新建文件spring.factories

内容为:org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.cdchen.HelloAutoConfiguration

6 使用starter 在 springboot 项目中引入


            com.cdchen
            hello-spring-boot-starter
            1.0-SNAPSHOT
        

7 增加配置文件

cdchen.msg=wo shi hello service

8 启动测试

@Component
public class ApplicationTestRunner implements ApplicationRunner, Ordered {
    @Autowired
    private HelloService helloService;
    @Override
    public int getOrder(){
        return 1;//通过设置这里的数字来知道指定顺序
    }

    @Override
    public void run(ApplicationArguments var1) throws Exception{
        System.out.println("MyApplicationRunner1!");
        helloService.sayHello();
    }

}

你可能感兴趣的:(springCloud,面试题)