日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

手写简单的启动器

發(fā)布時間:2023/12/10 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 手写简单的启动器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

starter

    • 1. target
    • 2. 手寫啟動器~
      • 2.1 自動裝配,自定義屬性
      • 2.2 啟動器,引用自動裝配模塊
    • 3. 在自己的項目引用上面的starter

1. target

1. 啟動器只用來做依賴導入(導入配置模塊)2. 專門來寫一個自動配置模塊3. 啟動器依賴自動配置;別人只需要引入啟動器(starter)xxx-spring-boot-starter; 自定義啟動器名分析: 一個啟動器需要兩個模塊,一個是自動裝配模塊,自動裝配屬性,定義好裝配規(guī)則。 第二個是啟動器引用自動配置模塊。 將兩個項目打包到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;}}

創(chuàng)建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

  • 創(chuàng)建springboot項目,引入依賴
  • 編寫controller
  • package cn.bitqian;import cn.bitqian.starter.HelloService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class DemoController {@AutowiredHelloService helloService;@RequestMapping("/hello")public String hello() {return "hello, " + helloService.sayHello(" rye ");}}
  • 自動裝配, 嚶嚶嚶
  • bitqian.hello.prefix=i like bitqian.hello.suffix=so much
  • so- - -
  • 總結

    以上是生活随笔為你收集整理的手写简单的启动器的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。