目录结构为
新建一个maven项目,在pom添加
<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-starter</artifactId>
</dependency>
</dependencies>
添加实体配置类
@ConfigurationProperties(prefix = "persion")
public class PersionProperties {
private String name;
private Integer age;
private Integer hight;
...省略get set方法
}
添加service类
public class PersionService {
private PersionProperties persionProperties;
public PersionService(PersionProperties persionProperties) {
this.persionProperties = persionProperties;
}
public String getInfo() {
return "姓名:" + persionProperties.getName() +
" 年纪:" + persionProperties.getAge() +
" 身高:" + persionProperties.getHight();
}
}
添加配置类
@Configuration
@EnableConfigurationProperties(PersionProperties.class)
public class PersionAutoConfig {
@Autowired
PersionProperties persionProperties;
@Bean
public PersionService persionService(){
PersionService persionService = new PersionService(persionProperties);
return persionService;
}
}
接下来最重要的一步
#-------starter自动装配---------
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.it.config.PersionAutoConfig
在该项目路径下进行maven打包、mvn clean install
测试:
新建一个测试项目、引入刚打包的包
<dependency>
<groupId>com.example</groupId>
<artifactId>test-springboot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
application.properties配置
persion.hight=175
persion.name=你好
persion.age=18
controller层
@RestController
public class TestController {
@Autowired
PersionService persionService;
@GetMapping("/getInfo")
public String getInfo(){
return persionService.getInfo();
}
}
在浏览器上访问
乱码问题暂不解决,创建springboot-starter基本流程完毕