How to customize starter

In the series of articles springboot start the process , we have seen the autoconfiguration mechanism springboot of paper will automatically configure a custom-based auto configuration mechanism of starter examples

 

text

Modular structure

First, we prepared two service modules and web, such as modular structure

 

service module

In the service module, we add a service class TestService

public class TestService {

    public String getName() {
        return "lay";
    }
}

 

Then add a ServiceAutoConfiguration class configuration, will become a Bean TestService

public class ServiceAutoConfiguration {


    @Bean
    public TestService testService() {
        return new TestService();
    }
}

 

And most important step will ServiceAutoConfiguration added to the META-INF / spring.factories becomes automatic configuration classes

org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.lay.service.ServiceAutoConfiguration

 

In this way, service just finished, we look at the structure of the service module

 

web module

Let's add a service module dependencies in pom.xml file in the web

<dependency>
    <groupId>cn.lay</groupId>
    <artifactId>service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

 

Add a TestController

@RestController
public class TestController {

    @Autowired
    private TestService testService;

    @GetMapping("getName")
    public String getName() {
        return testService.getName();
    }
}

 

Start the test

When we started the web, will be automatically configured according to the type of service META-INF / spring.factories configured to become TestService Bean, and then injected into the TestController

We open the browser test

We can see that we get the result of the successful implementation

 

Add notes conditioning

Add in the META-INF / spring.factories configured in class, but many times we want to configure the class will take effect when certain conditions are met, for which we can use @Conditional comment or a combination of the @Conditional annotated notes

In this example, we will use @ConditionalOnClass use annotations in ServiceAutoConfiguration, the annotation indicates that when there is a class when the class configuration takes effect

@ConditionalOnClass(name = "java.lang.String")
public class ServiceAutoConfiguration {


    @Bean
    public TestService testService() {
        return new TestService();
    }
}

注解中指明,当String类存在的时候ServiceAutoConfiguration才会进行解析处理,而如果你指明一个不存在的类,如:java.lang.String2,那么TestService将不会被解析成一个Bean

 

添加properties

我们先在service模块的pom.xml中添加properties处理依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

 

添加一个配置属性对象

@ConfigurationProperties(prefix = "service")
public class ServiceProperties {
    private String name;

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "ServiceProperties{" +
                "name='" + name + '\'' +
                '}';
    }
}

 

在ServiceAutoConfiguration添加为Bean,开启属性自动解析

@ConditionalOnClass(name = "java.lang.String")
@EnableConfigurationProperties(value = {ServiceProperties.class})
public class ServiceAutoConfiguration {


    @Bean
    public TestService testService() {
        return new TestService();
    }
}

 

把ServiceProperties注入到TestService中

public class TestService {

    @Autowired
    private ServiceProperties serviceProperties;

    public String getName() {
        return serviceProperties.getName();
    }
}

 

回到web模块,在application.properties中添加配置

service.name=lay

重新启动测试,你会看到浏览器获取到了结果

 

总结

springboot的自动配置,其实就是包了一层自动发现(spi机制)的spring.factories配置,然后通过@Conditional相关的条件化处理来决定是否进行配置类的解析工作。

 

Guess you like

Origin www.cnblogs.com/lay2017/p/11518240.html