spring boot(一):Hello World,springhello

來源:互聯網
上載者:User

spring boot(一):Hello World,springhello
前言

          作為程式員,不管是.net程式員還是java程式員其實從骨子裡都不太喜歡各種設定檔的,記得剛開始學java SSH時動不動就裝B,來看看我的配置多不多,又是從.net開始寫java的程式員提起各種spring設定檔更是頭大,那麼Spring Boot誕生了,Spring Boot的誕生只為在當前流行的微服務架構下簡化開發,不用再一上來就是各種設定檔了。

          老生常談,先從Hello World寫起。本篇基於idea、maven搭建spring boot開發環境。

 

項目結構

         先看下項目大致結構,基本骨架和ssm的項目結構保持相同,不同的是多了一個Application.java類,建議放在default package下。

 

基於idea+maven建立spring boot項目

1、我建立項目時,spring boot的最新版本是1.5.4

2、使用預設的maven項目建立,不勾選任何骨架。

OK、這樣一個預設的maven項目就起來了,當然,當前還不叫一個spring boot程式,開啟pom.xml,增加spring boot需要的依賴包。最主要的就是spring-boot-starter-parent、

spring-boot-start-web。只需兩個依賴就能建立一個spring mvc程式。 是不是很happy。

<?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>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>1.5.4.RELEASE</version>    </parent>    <groupId>com.autohome</groupId>    <artifactId>springbootdemo</artifactId>    <version>1.0-SNAPSHOT</version>    <packaging>war</packaging>    <name>springbootdemo</name>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>    </dependencies></project>

  

3、也可以用Spring Initializer骨架建立

     基於Spring Initializer建立spring boot項目更便捷,會直接讓你選擇你需要的模組,比如AOP、Web、JPA之類。不過也看到了,這裡依賴https://start.spring.io。我建立時提示我連不上伺服器,直接看不到下一步的介面了,so sorry。

 

Spring Boot Hello World

 User.java

public class User {    private Integer id;    private String name;    private String address;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address;    }}

UserController.java

package com.autohome.controller;import com.autohome.model.User;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controller@RequestMapping("/user")public class UserController {    @ResponseBody    @RequestMapping("/detail")    public  User detail(Integer id){        User user=new User();        user.setId(id);        user.setName("zhangsan");        user.setAddress("china");        return user;    }}

 Application.java

  這裡用到了三個註解屬性:

  SpringBootApplication:它是Configuration、ComponentScan、EnableAutoConfiguration三個註解集合。 也就是說使用@SpringBootApplication(scanBasePackages = "com.autohome") 就可以替代前面三個註解,算是spring文法糖。

 

  ComponentScan:會自動掃描指定包下含有註解屬性的類,比如@Service、@Controller、@Repository。

  EnableAutoConfiguration:能夠自動設定上下文,試圖猜測和配置你想要的類。

 

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ComponentScan;/** * Created by zhangfei on 2017/6/22. */@SpringBootApplication@ComponentScan("com.autohome")@EnableAutoConfigurationpublic class Application {    public static void main(String[] args){        System.out.println("server is running at 8080....");        SpringApplication.run(Application.class,args);    }}

 

  

經過以上幾句代碼,右鍵運行Application.java,看控制台提示

 

瀏覽器輸入:http://localhost:8080/user/detail?id=1

瀏覽器輸出: {"id":1,"name":"zhangsan","address":"china"}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.