下面创建一个 spring-boot-starter-hello 的启动器,这个启动器的Person与配置文件的属性绑定,可以供引入这个启动器的项目进行配置。HelloService 使用sayHello方法输出Person内容。HelloAautoConfiguration是配置类,产生HelloService的bean。
项目结构:
首先创建一个SpringBoot项目,pom要做一些修改:
添加如下两个依赖:
<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>
spring-boot-starter:包含自动配置的基本包,spring-boot-configuration-processor:添加这个依赖会在配置文件产生提示。
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.19.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.cld</groupId>
<artifactId>spring-boot-starter-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<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>
</dependencies>
</project>
Person.java
package cn.cld;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "person")
public class Person {
private String id="DEFAULT";
private String name="DEFAULT1";
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}
@ConfigurationProperties(prefix = "person") 把Person类和配置文件绑定。
HelloService.java
package cn.cld;
public class HelloService {
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String sayHello(){
return "Say : "+this.person;
}
}
HelloAautoConfiguration.java
package cn.cld;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(Person.class)
@ConditionalOnWebApplication
@ConditionalOnClass(HelloService.class)
@ConditionalOnProperty(prefix = "person",value = {"id","name"},matchIfMissing=true)
public class HelloAautoConfiguration {
@Autowired
private Person person;
@Bean
@ConditionalOnMissingBean(HelloService.class)
public HelloService helloService(){
HelloService hs=new HelloService();
hs.setPerson(person);
return hs;
}
public int getOrder(){
return 0;
}
}
@EnableConfigurationProperties(Person.class) 把与配置文件绑定的Person类导入到容器;
@ConditionalOnWebApplication 只有WEB应用,该配置类才生效;
@ConditionalOnClass(HelloService.class) 只有存在HelloService这个类配置类才生效;
@ConditionalOnProperty(prefix = "person",value = {"id","name"},matchIfMissing=true) 配置中存在person前缀,名称有id有name时候才生效,true说明即使不存在也生效,false不存在报错。
@ConditionalOnMissingBean(HelloService.class)容器里不存在HelloService的bean时候才创建该Bean.
最后创建META-INF目录在里面创建如下文件
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.cld.HelloAautoConfiguration
最后,其他SpringBoot项目引入这个自定义启动器,就可以注入HelloService进行sayHello调用,并可以在配置文件里配置person的值。