新建一个maven项目
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.20.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>simple-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>simple-spring-boot-starter</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- 注意:下面这个插件不能有,否则打的包没法使用 -->
<build>
<!--<plugins>-->
<!--<plugin>-->
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-maven-plugin</artifactId>-->
<!--</plugin>-->
<!--</plugins>-->
</build>
</project>
配置信息:
@ConfigurationProperties(prefix = "simple")
public class SimpleProperties {
private String name = "XXX";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
功能实现:
public class SimpleService {
private String name;
public String printName(){
return "The name is : " + name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
导入配置:
@Configuration
@EnableConfigurationProperties(SimpleProperties.class)
@ConditionalOnClass(SimpleService.class)
public class SimpleAutoConfigure {
@Autowired
private SimpleProperties simpleProperties;
@Bean
@ConditionalOnMissingBean(SimpleService.class)
public SimpleService simpleService() {
// @ConditionalOnMissingBean:当spring容器中没有这个Bean的时候才进行下一步
SimpleService simpleService = new SimpleService();
simpleService.setName(simpleProperties.getName());
return simpleService;
}
}
src/resources/META-INF/ 下新建文件 spring.factories 写入以下内容:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.SimpleAutoConfigure
mvn:install 打包到本地仓库中
测试一下
再新建一个maven项目,pom.xml中引入上面的jar包
<dependency>
<groupId>com.example</groupId>
<artifactId>simple-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
application.properties 配置文件中配置:simple.name=wanggq
编写测试代码:
@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class SimpleStarterTest {
@Autowired
private SimpleService simpleService;
@Test
public void test(){
System.out.println(simpleService.printName());
}
}
输出如下:
The name is : wanggq