Spring Self Starter and Problem Solved

Create a new Maven project, names as string-spring-boot-starter:
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.test.springboot</groupId>
  <artifactId>string-spring-boot-starter</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starters</artifactId>
    <version>2.1.4.RELEASE</version>
  </parent>

  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <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>

</project>

Then you can add two class(com.test.springboot.configure.StringAutoConfigurecom.test.springboot.service.impl.StringServiceImpl) and one interface(com.test.springboot.service.IStringService) and one configure file(spring.factories
under resources\META-INFdirectory )

IStringService you can define an interface:

List<String> split(String value);

StringServiceImpl you can implementation the inteface:

@Override
public List<String> split(String value) {
    return Stream.of(StringUtils.split(value, ",")).collect(Collectors.toList());
  }

StringAutoConfigure you can add the configuration:

@Configuration
@ConditionalOnClass(value = {IStringService.class, StringServiceImpl.class})
public class StringAutoConfigure {

  @Bean
  @ConditionalOnMissingBean
  IStringService starterService() {
    return new StringServiceImpl();
  }
}

spring.factories you can import the configuration:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.test.springboot.configure.StringAutoConfigure

under the root directory of the project, in the terminal, input command:

D:\Project\SpringCloud\string-spring-boot-starter> mvn clean install -Dmaven.test.skip=true

and you can see the generation like following:

 Installing D:\workbench\Project\SpringCloud\string-spring-boot-starter\pom.xml to C:\Users\Administrator\.m2\repository\com\test\springboot\string-spring-boot-starter\1.0-SNAPSHOT\string-spring-boot-starter-1.0-SNAPSHOT.pom

Then change the project

In the target project ,import your starter in pom.xml

<!-- User defined starter -->
        <dependency>
            <groupId>com.test.springboot</groupId>
            <artifactId>string-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

then you can write a junit test

@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class StringServiceTest {

  @Autowired
  private IStringService stringService;

  @Autowired
  private SpringBootConfig springBootConfig;

  @Test
  public void testStringVersion() {
    log.info("String version: {}", JSON.toJSONString(
        stringService.split(springBootConfig.getVersion())
    ));
  }
}

first running, throw exception:

NoSuchBeanDefinitionException interface  IStringService 

Reason is in the string-spring-boot prject , META-INF, I wrote it as META_INF, so target project can not import the bean, correct it and run again.

run result will be:

2020-05-20 10:30:23.864  INFO 14156 --- [           main] c.i.s.study.service.StringServiceTest     : String version: ["2.1","2.1.4"]

finished!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值