手写简单的启动器
starter
- 1. target
- 2. 手寫啟動器~
- 2.1 自動裝配,自定義屬性
- 2.2 啟動器,引用自動裝配模塊
- 3. 在自己的項目引用上面的starter
1. target
1. 啟動器只用來做依賴導入(導入配置模塊)2. 專門來寫一個自動配置模塊3. 啟動器依賴自動配置;別人只需要引入啟動器(starter)xxx-spring-boot-starter; 自定義啟動器名分析: 一個啟動器需要兩個模塊,一個是自動裝配模塊,自動裝配屬性,定義好裝配規則。 第二個是啟動器引用自動配置模塊。 將兩個項目打包到maven倉庫,或者私服倉庫, 自己的其它項目引用啟動器就行了, 在yaml或者properties中配置好啟動器的屬性就行了。需要的注解
@Configuration 指定這是一個配置類@ConditionalOnXXX 指定條件成立下,自動配置類生效@AutoConfigureAfter 指定自動配置的順序@Bean 給容器中添加組件@ConfigurationProperites 結合xxxProperties 類綁定相關的配置@EnableConfigurationProperties // 讓xxxProperties生效加入容器中自動配置類如何能加載將需要啟動就加載的自動配置類,配置在META-INF/spring.properteis 中2. 手寫啟動器~
2.1 自動裝配,自定義屬性
自動配置,pom,只留下下面的依賴
<?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 https://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>2.3.4.RELEASE</version><relativePath/> </parent><!-- 自動配置模塊... --><groupId>cn.bitqian</groupId><artifactId>bitqian-spring-boot-starter-autoconfigurer</artifactId><version>0.0.1-SNAPSHOT</version><name>bitqian-spring-boot-starter-autoconfigurer</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></dependencies></project>啟動器的屬性,boot中常見的xxxProperties類
package cn.bitqian.starter;import org.springframework.boot.context.properties.ConfigurationProperties;@ConfigurationProperties(prefix = "bitqian.hello") // 配置別名 public class HelloProperties {/*** 可以配置的屬性:前綴*/private String prefix;/*** 可以自動裝配的屬性:后綴*/private String suffix;public String getPrefix() {return prefix;}public void setPrefix(String prefix) {this.prefix = prefix;}public String getSuffix() {return suffix;}public void setSuffix(String suffix) {this.suffix = suffix;} }啟動器功能方法
package cn.bitqian.starter;/*** @author echo lovely* @date 2020/11/8 11:39*/ public class HelloService {private HelloProperties helloProperties;public HelloProperties getHelloProperties() {return helloProperties;}public void setHelloProperties(HelloProperties helloProperties) {this.helloProperties = helloProperties;}/*** 啟動器的方法,配置前后綴,實現sayHello* @param name parameter* @return string*/public String sayHello(String name) {return helloProperties.getPrefix() + name + helloProperties.getSuffix();}}自動裝配類
package cn.bitqian.starter;import org.springframework.beans.factory.annotation.Autowired; 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 @ConditionalOnWebApplication // web應用才能生效 @EnableConfigurationProperties(HelloProperties.class) // 讓屬性文件生效 public class HelloServiceAutoConfiguration {@Autowiredprivate HelloProperties helloProperties;@Beanpublic HelloService helloService() {HelloService helloService = new HelloService();helloService.setHelloProperties(helloProperties);return helloService;}}創建META-INF文件夾,配置自動裝配類,在spring.factories里面
# Auto Configure org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ cn.bitqian.starter.HelloServiceAutoConfiguration雙擊安裝到maven倉庫
2.2 啟動器,引用自動裝配模塊
啟動器模塊,依賴自動配置模塊
<?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><!-- 啟動器 --><groupId>cn.bitqian</groupId><artifactId>bitqian-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version><!-- bitqian springboot 啟動器,依賴導入 --><dependencies><dependency><groupId>cn.bitqian</groupId><artifactId>bitqian-spring-boot-starter-autoconfigurer</artifactId><version>0.0.1-SNAPSHOT</version></dependency></dependencies></project>安裝到maven倉庫
3. 在自己的項目引用上面的starter
總結
- 上一篇: 一文了解plc编程、电脑编程、手机APP
- 下一篇: java的日期操作_java中对时间的操