快速入门自定义Starter
[TOC]
1.概念
依赖管理是任何复杂项目的关键所在,当项目布局比较大时,纯手工的去做依赖显然比较浪费时间和精力,其中产生各种小问题同样会减少工作核心部分的投入时间
Springboot starter作为Springboot四大神器之一,就是为了解决这一问题而诞生,分而治之,提高代码复用性,让代码的配置看起来更加简洁
(与Apollo结合使用将大大降低因配置而产生的维护难度,提高了配置的治理效率)
2.思路
1.引入SpringBoot配置自动注入的依赖*2
1 2 3 4 5 6 7 8 9
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-autoconfigure</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
|
2.创建配置类(定义properties的内容)
3.创建服务类(需要封装和处理的业务逻辑等)
4.创建自动配置类(将配置类和服务类注入)
5.创建resources/META-INF/spring.factories工厂配置文件,注入自动配置类
1
| org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.dony15.config.PersonServiceAutoConfiguration
|
3.搭建
3-1.配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.dony15.config;
import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix="person.proterties.set") public class PersonServiceProperties { private String name; private int age; private String sex = "man"; private String height; private String weight; ...get/set略 }
|
3-2.服务类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.dony15.service;
import com.dony15.config.PersonServiceProperties;
public class PersonService { private PersonServiceProperties personServiceProperties;
public PersonService(PersonServiceProperties personServiceProperties){ this.personServiceProperties=personServiceProperties; } public PersonService() { } public String getPersonName(){ return personServiceProperties.getName(); } public int getPersonAge(){ return personServiceProperties.getAge(); }
public String getPersonSex(){ return personServiceProperties.getSex(); } }
|
3-3.自动配置类(核心)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.dony15.config;
import com.dony15.service.PersonService; 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.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
@Configuration @EnableConfigurationProperties(PersonServiceProperties.class) @ConditionalOnClass(PersonService.class) @ConditionalOnProperty(prefix = "person.proterties.set",value = "enabled",matchIfMissing = true) public class PersonServiceAutoConfiguration { @Autowired private PersonServiceProperties properties;
@Bean @ConditionalOnMissingBean(PersonService.class) public PersonService personService(){ return new PersonService(properties); } }
|
4.使用
至此,一个简单的starter搭建完毕,如下可以引用
maven
1 2 3 4 5
| <dependency> <groupId>com.dony15</groupId> <artifactId>demo-starter</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
|
properties
1 2 3 4
| person.proterties.set.age=18 person.proterties.set.name=常贵 person.proterties.set.height=189 ...
|
然后直接Autowired自动注入即可正常使用