【Spring Boot】2.HelloWorld應用程序

HelloWorld應用程序

  1. 給maven的settings.xml配置文件的profiles標籤添加
<profile>
        <id>jdk‐1.8</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <jdk>1.8</jdk>
        </activation>
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        </properties>
    </profile>
  1. 在maven中添加sprig boot相關依賴
<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  1. 編寫一個主程序,啓動spring boot應用
package com.zhaoyi.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloWorldApplication {
    public static void main(String[] args){
        SpringApplication.run(HelloWorldApplication.class, args);
    }
}

程序探究

一、POM文件

1 父項目

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>]

父項目才真正的管理Spring Boot應用裏面的全部依賴版本。css

Spring boot的版本仲裁中心: 之後咱們導入依賴默認是不須要寫版本(沒有在dependency裏面管理的依賴天然須要聲明版本號)java

二、導入的依賴

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

一、啓動器

spring-boot-starter: spring-boot場景啓動器;如spring-boot-starter-web則幫咱們導入了web模塊須要正常運行所依賴的組件。 參照官方文檔咱們能夠發現,spring-boot-starter-*表明了一系列的功能場景,當你須要什麼具體業務需求的時候,將其導入就能夠了,好比aop、data-redis、mail、web等。他將全部的功能場景都抽出來,作成一個個的starter(啓動器),其先關的業務場景的全部依賴都會導入進來,要用什麼功能就導入什麼場景的啓動器。web

二、主程序類

package com.zhaoyi.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloWorldApplication {
    public static void main(String[] args){
        SpringApplication.run(HelloWorldApplication.class, args);
    }
}

@SpringBootApplication SB應用標註在某個類上說明這個類是SB的主配置類,SB就應該運行這個類的main方法來啓動SB應用。redis

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {

@SpringBootConfiguration SB的配置類; 標註在某個類上,表示這個類是SB的配置類。spring

@Configuration 配置類上來標註這個註解; 配置類就是配置文件;配置類也是容器中的一個組件;@companent瀏覽器

@EnableAutoConfiguration 開啓自動配置功能; 之前咱們須要在SP中配置的東西,SB幫咱們自動配置,經過此配置告訴SB開啓自動配置功能。這樣,自動配置的功能才能生效。app

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({EnableAutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

    Class<?>[] exclude() default {};

    String[] excludeName() default {};
}

@AutoConfigurationPackage 自動配置包 SP的底層註解@import,給容器中導入一個組件。 將主配置類(@SpringBottApplication標註的類)所在包及下面全部子包裏面的全部組件掃描到SP容器。maven

@Import({EnableAutoConfigurationImportSelector.class}) 導入哪些組件的選擇器;將全部須要導入的組件以全類名的方式返回,這些組件就會被添加到容器中; 會給容器中導入很是多的自動配置類(xxxAutoConfiguration);就是給容器中導入這個場景須要的全部組件,並配置好這些組件。這樣就免去了咱們手動編寫配置注入功能組件等的工做;spring-boot

SB在啓動的時候從類路徑下的META-INFO/spring.factories中獲取EnableAutoConfiguration指定的值,將這些紙做爲自動配置類導入容器中,自動配置類就生效,幫咱們進行自動配置工做;之前咱們須要本身配置的東西,自動配置類都幫咱們作了。code

J2EE的總體解決方案和自動配置都在sprig-boot-autoconfigure.jar中了;

使用Spring Initializer快速建立SB項目

IDE都支持使用Spring的項目建立嚮導; 選擇咱們須要的模塊,嚮導會聯網建立SB項目;

@ResponseBody 這個類的全部方法(或者單個方法——)返回的數據直接寫給瀏覽器。 @ResponseBody寫在類上面時,大能夠寫爲@RestController,其等價於@ResponseBody+@Controller;

默認生成的項目

  • 主程序已經生成好了,咱們只需關注本身的業務邏輯
  • resounces文件夾中的目錄結構
    • static:保存全部的靜態資源:js css images;
    • templates:保存全部的模板頁面;(SB默認jar包,使用嵌入式頁面;可使用模板引擎——freemarker、thymeleaf)
    • application.properties SB應用的配置文件;SB的一些默認配置能夠在這裏進行修改。
相關文章
相關標籤/搜索