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.StringAutoConfigure
和com.test.springboot.service.impl.StringServiceImpl
) and one interface(com.test.springboot.service.IStringService
) and one configure file(spring.factories
under resources\META-INF
directory )
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!