Spring Boot-Starter(九)

说明

在使用非spring boot项目我们集成spring mvc mybatis等框架往往需要大量xml配置, spring 的推出是为了解决项目的复杂度,随着项目的增长,xml配置会越来越臃肿,

所以spring 推出了spring boot 使用自动化配置 通过javebean的配置方式为我们配置常用的配置方式,如果有定制需求 可以覆盖默认配置。实现方式则是根据Starter

自己实现一个Starter

Spring 官方 Starter通常命名为 spring-boot-starter-{name} 如 spring-boot-starter-web 

Spring官方建议非官方Starter命名应遵循 {name}-spring-boot-starter 的格式

创建一个项目

2.引入pom依赖

<dependencies>
        <!-- @ConfigurationProperties annotation processing (metadata for IDEs)
                生成spring-configuration-metadata.json类,需要引入此类 主要用于ideal 通过点击yml或者properties属性跳转到你的配置类-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
    </dependencies>

创建配置类

@ConfigurationProperties("example.service")
public class TestProperties {
    private String name;
    private String url;

    public String getName() {
        return name;
    }

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

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

里面还可以配置对象或者map

通过example.service.[mapObjectName].[keyName]=valie

通过example.service.[ObjectName].[filedName]=valie

创建Service类

public class TestService {
    private TestProperties testProperties;

    public TestService(TestProperties testProperties){
        this.testProperties=testProperties;
    }
}

创建配置类

@Configuration
@ConditionalOnClass(TestService.class)//当Spring Context中不存在该Bean时。
@EnableConfigurationProperties(TestProperties.class)
public class TestAutoConfigure {

    private final TestProperties properties;

    @Autowired
    public TestAutoConfigure(TestProperties properties) {
        this.properties = properties;
    }

    @Bean
    @ConditionalOnMissingBean//当classpath下发现该类的情况下进行自动配置。
    @ConditionalOnProperty(prefix = "test.service", value = "enabled",havingValue = "true")//当配置文件中example.service.enabled=true时。
   TestService exampleService (){
        return  new TestService(properties);
    }
}

创建spring.factories

META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=
test.teststarter.TestAutoConfigure

如果多个配置类,号隔开

更多Conditional配置

@ConditionalOnBean:当容器中有指定的Bean的条件下  
@ConditionalOnClass:当类路径下有指定的类的条件下  
@ConditionalOnExpression:基于SpEL表达式作为判断条件  
@ConditionalOnJava:基于JVM版本作为判断条件  
@ConditionalOnJndi:在JNDI存在的条件下查找指定的位置  
@ConditionalOnMissingBean:当容器中没有指定Bean的情况下  
@ConditionalOnMissingClass:当类路径下没有指定的类的条件下  
@ConditionalOnNotWebApplication:当前项目不是Web项目的条件下  
@ConditionalOnProperty:指定的属性是否有指定的值  
@ConditionalOnResource:类路径下是否有指定的资源  
@ConditionalOnSingleCandidate:当指定的Bean在容器中只有一个,或者在有多个Bean的情况下,用来指定首选的Bean @ConditionalOnWebApplication:当前项目是Web项目的条件下  

测试

将项目打包 另外一个项目依赖并配置

1.配置

example:
  service:
    name: "dd"
    url: "ccc"
test:
  service:
    enabled: true

2.运行unit test

@RunWith(SpringRunner.class)
@SpringBootTest
public class MybatisSourceTestApplicationTests {
    @Autowired
    TestService testService;

    @Test
    public void contextLoads() {
        System.out.print(testService.toString());
    }

}
原文地址:https://www.cnblogs.com/LQBlog/p/10670126.html