【Spring Boot學習之一】Spring Boot簡介

環境
  Java1.8
  Spring Boot 1.3.2java

1、Spring Boot特色
一、使用java運行項目,內置tomcat,無需外部容器;
二、減小XML配置,使用properties文件和註解;
三、Spring Boot對各個Spring技術進行了封裝,並非產生新技術,方便使用了Spring;web

趨勢:面向服務架構(SOA)轉向微服務架構,
四、Spring Cloud基於Spring Boot,Spring Cloud(HTTP+rest),基於Spring Boot中WEB組件(Spring MVC);spring

2、入門項目
一、搭建json

new-->maven project瀏覽器

二、依賴
利用Maven繼承依賴的特性,向上層級引入Spring Boot的其餘依賴包。
(1)spring-boot-starter-parent做用
在pom.xml中引入spring-boot-start-parent,spring官方的解釋叫什麼stater poms,它能夠提供dependency management,也就是說依賴管理,引入之後在申明其它dependency的時候就不須要version了,後面能夠看到。
(2)spring-boot-starter-web做用
SpringWEB核心組件(springMVC + Spring)
(3)spring-boot-maven-plugin做用
若是咱們要直接Main啓動spring,那麼如下plugin必需要添加,不然是沒法啓動的。若是使用maven 的spring-boot:run的話是不須要此配置的。(我在測試的時候,若是不配置下面的plugin也是直接在Main中運行的。) tomcat

三、編寫HelloWord服務架構

package com.wjy.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@EnableAutoConfiguration
@RestController
public class HelloController {
    
    @RequestMapping("/hello")
    public String hello() {
        return "hello world";
    }
    
    public static void main(String[] args) {
        SpringApplication.run(HelloController.class, args);
    }

}

 

(1)@RestController
在上加上RestController 表示修飾該Controller全部的方法返回JSON格式,直接能夠編寫Restful接口  至關於@Controller+@ResponseBody app

(2)@EnableAutoConfiguration
做用在於讓Spring Boot根據應用所聲明的依賴來對Spring框架進行自動配置:
這個註解告訴Spring Boot根據添加的jar依賴猜想你想如何配置Spring。因爲spring-boot-starter-web添加了Tomcat和Spring MVC,因此auto-configuration將假定你正在開發一個web應用並相應地對Spring進行設置。框架

(3)SpringApplication.run(HelloController.class, args) 標識爲啓動類maven

(4)@ResponseBody 用在方法上將返回內容轉換成json格式

(5)@SpringBootApplication = (默認屬性)@Configuration + @EnableAutoConfiguration + @ComponentScan。
做用:啓動項目 整合經常使用註解 掃包做用(掃描當前同級包及其子包)
@ComponentScan默認狀況下:它掃描全部類型(@Service,@Repository,@Component,@Controller),而且掃描範圍是@ComponentScan註解所在配置類包及子包的類

四、啓動
默認端口8080
(1)啓動方式一  啓動單個controller

package com.wjy.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@EnableAutoConfiguration
@RestController
public class HelloController {
    
    @RequestMapping("/hello")
    public String hello() {
        return "hello world";
    }
    
    public static void main(String[] args) {
        SpringApplication.run(HelloController.class, args);
    }

}

 

(2)啓動方式二 掃包啓動一片controller

package com.wjy.base;

import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages="com.wjy.controller")
public class App {

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

}

 

瀏覽器訪問驗證:

 

相關文章
相關標籤/搜索