<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 一、概述 SpringBoot核心思想是**約定大于配置**,它是和Spring框架緊密結合用于提升Spring開發者體驗的工具。同時它集成了大量常用的第三方庫配置(例如Jackson, JDBC, Mongo, Redis, Mail等等),SpringBoot應用中這些第三方庫幾乎可以零配置的開箱即用(out-of-the-box),大部分的SpringBoot應用都只需要非常少量的配置代碼,開發者能夠更加專注于業務邏輯。 ## 二、spring-boot-starter SpringBoot能夠在配置上相比Spring要簡單許多, 其核心在于spring-boot-starter, 在使用SpringBoot來搭建一個項目時,只需要引入官方提供的starter, 就可以直接使用,免去了各種配置; SpringBoot存在很多開箱即用的 Starter依賴,使得我們在開發業務代碼時能夠不需要過多關注框架的配置,而只需要關注業務即可;舉例來說,SpringBoot項目中集成 Redis,只需要加入 spring-data-redis-starter 的依賴,并簡單配置一下連接信息以及 Jedis 連接池配置就可以,省去了之前很多的配置操作。甚至有些功能的開啟只需要在啟動類或配置類上增加一個注解即可完成; ## **原理** 在SpringBoot應用開發中,當我們在pom文件中引入starter組件依賴坐標時,啟動Spring容器,此時應用會去依賴的Starter包中查找resources/META-INF/spring.factories文件,根據文件中配置去加載相應的自動配置類,類似于Java的SPI機制。簡單的說自動配置是根據約定來的,也就是說SpringBoot要求必須按照他給的規則開發starter才會幫你自動配置。 ## **自定義** 1、新建一個SpringBoot工程,命名為spring-boot-starter-hello,pom.xml依賴; ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 2、新建HelloProperties類,定義一個hello.msg參數(默認值World!); ``` @ConfigurationProperties(prefix = "hello") public class HelloProperties { /** * 打招呼的內容,默認為“World!” */ private String msg = "World!"; public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } } ``` 3、新建HelloService類,使用HelloProperties類的屬性; ``` @Service public class HelloService { @Autowired private HelloProperties helloProperties; /** * 打招呼方法 * @param name 人名,向誰打招呼使用 * @return */ public String sayHello(String name) { return "Hello " + name + " " + helloProperties.getMsg(); } } ``` 4、自動配置類,可以理解為實現自動配置功能的一個入口; ``` //定義為配置類 @Configuration //在web工程條件下成立 @ConditionalOnWebApplication //啟用HelloProperties配置功能,并加入到IOC容器中 @EnableConfigurationProperties({HelloProperties.class}) //導入HelloService組件 @Import(HelloService.class) //@ComponentScan public class HelloAutoConfiguration { } ``` 5、在resources目錄下新建META-INF目錄,并在META-INF下新建spring.factories文件,寫入: ``` org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.springbootstarterhello.HelloAutoConfiguration ``` 6、收尾 ``` 1、刪除自動生成的啟動類SpringBootStarterHelloApplication。 2、刪除resources下的除META-INF目錄之外的所有文件目錄。 3、刪除spring-boot-starter-test依賴并且刪除test目錄。 ``` 7、執行mvn install將spring-boot-starter-hello安裝到本地; 當你直接執行時應該會報錯,因為我們還需要在pom.xml去掉spring-boot-maven-plugin,也就是下面這段代碼: ``` <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> ``` ## **使用** 隨便新建一個SpringBoot工程,引入spring-boot-starter-hello依賴。 ``` <dependency> <groupId>com.example</groupId> <artifactId>spring-boot-starter-hello</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> ``` .在新工程中使用spring-boot-starter-hello的sayHello功能。 ``` @SpringBootApplication @Controller public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Autowired private HelloService helloService; @RequestMapping(value = "/sayHello") @ResponseBody public String sayHello(String name){ System.out.println(helloService.sayHello(name)); return helloService.sayHello(name); } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看