SpringBoot學習(二)

SpringBoot1.x環境搭建

1)項目中只須要依賴父工程spring-boot-starter-parent便可,該父工程中管理了不少springboot須要的依賴包及其版本。web

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/>
</parent>

relativePath的做用:spring

默認值爲../pom.xml, 查找依賴父項目的順序爲: relativePath元素中的地址 - 本地倉庫 - 遠程倉庫tomcat

<relativePath/> 就是配置了一個空的元素,意思是:始終從倉庫中獲取,不從本地路徑獲取。springboot

2)添加spring-boot-starter-parent父工程後,項目若是須要使用springmvc和spring的jar包,只須要添加以下依賴:mvc

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

 

不須要添加版本,由於版本號在父工程中統一管理了app

3)寫第一個入門程序spring-boot

//開啓Springboot的默認自動配置
@EnableAutoConfiguration
@Controller
public class IndexController {

    @RequestMapping("/")
    @ResponseBody
    public String first() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        /**
         * springboot的啓動方法
         * 第一個參數:當前文件的字節碼
         * 第二個參數:main方法的參數
         */
        SpringApplication.run(IndexController.class, args);
    }

}

main方法啓動便可,內嵌tomcat運行,端口號默認爲8080,啓動後頁面輸入localhost:8080/便可訪問spa

若是想關閉某個第三方類自動配置,能夠這樣排除:@EnableAutoConfiguration(exclude = {RedisAutoConfiguration.class})code

4)springboot的全局配置文件xml

名稱必須爲application.properties 或者是 application.yml,放在resources目錄下或者類路徑下的/config下,通常咱們放在resources下;

相關文章
相關標籤/搜索