# my-spring-boot-starter **Repository Path**: flyation/my-spring-boot-starter ## Basic Information - **Project Name**: my-spring-boot-starter - **Description**: 手写一个spring boot starter - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-03-31 - **Last Updated**: 2021-11-02 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README > 说明: > > Service类是自定义的一个类,该类中有成员变量prefix和suffix,成员方法doService,该方法的作用是对入参value加上前缀后prefix和后缀suffix后返回。 > > ServiceProperties类的作用是将配置文件application.yml中以my.service开头的配置读入。 > > ServiceAutoConfiguration类的作用是使ServiceProperties生效,并将一个Service的实例注入IoC容器。 > > 目标: > > 别的Spring Boot工程引用这个starter,通过自动装配来使用Service # 1.编写程序 ![image-20210331173656609](C:\Users\FLY\AppData\Roaming\Typora\typora-user-images\image-20210331173656609.png) ## java部分 autoconfigure包下存放`ServiceAutoConfiguration.java`、`ServiceProperties.java`,service包下存放`Service` ```java @Data @AllArgsConstructor public class Service { private String prefix; private String suffix; public String doService(String value) { return prefix + value + suffix; } } ``` ```java @Data @ConfigurationProperties(prefix = "my.service") public class ServiceProperties { private String prefix; private String suffix; } ``` ```java @Configuration @ConditionalOnClass(Service.class) // 类路径下存在Service时才生效 @EnableConfigurationProperties(ServiceProperties.class) // 使ServiceProperties生效 public class ServiceAutoConfiguration { @Autowired private ServiceProperties serviceProperties; @Bean @ConditionalOnMissingBean // 使这个bean成为单例模式 @ConditionalOnProperty( prefix = "my.service", value = "enable", havingValue = "true" ) // 当配置文件中my.service.enable的值为true时才生效 public Service service() { return new Service(serviceProperties.getPrefix(), serviceProperties.getSuffix()); } } ``` ## resources部分 resources下新建一个文件META-INF,在META-INF下新建文件`spring.factories`(必须叫这个名!!!) ``` # Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.myspringbootstarter.autoconfigure.ServiceAutoConfiguration ``` # 2.install打包安装到本地仓库 有两种方式,任选一种即可 ## 方式一:通过maven的springboot插件打包(便捷、推荐) pom.xml: ```xml org.springframework.boot spring-boot-maven-plugin true ``` 重要!spring boot工程创建时没有``true``这一行,install前需要加入这一行设置! 否则打出来的包中会多一个BOOT-INF文件夹,导致别的工程无法引用我们的类 ## 方式二:通过maven的apache maven插件打包 pom.xml: ```xml org.apache.maven.plugins maven-compiler-plugin 1.8 1.8 ``` ## 也可以通过IDEA手动打包后,再用maven手动安装到本地仓库 maven手动安装命令: ``` mvn install:install-file -DgroupId=com.example -DartifactId=my-spring-boot-starter -Dversion=1.0 -Dpackaging=jar -Dfile=E:\my-spring-boot-starter.jar ``` # 3.其他工程通过maven引入 在其他工程中: - pom.xml: ```xml com.example my-spring-boot-starter 0.0.1-SNAPSHOT ``` ![image-20210331182119533](C:\Users\FLY\AppData\Roaming\Typora\typora-user-images\image-20210331182119533.png) - application.yml: ```yml my: service: enable: true prefix: pp suffix: ss ``` 在controller中测试: ```java @RestController public class UserController { @Autowired private Service service; @RequestMapping("/service") public String service() { return service.doService("haha"); } } ``` 通过浏览器访问,返回ppp-haha-sss