springboot 入門教程(1)

前言

一、什麼是springboot?java

    簡單的理解就是一個快速開發框架集合,你們都叫他快速開發腳手架。web

二、springboot有什麼特徵?spring

     它遵循「習慣因爲配置」原則,使用springboot只須要少許配置,咱們大部分時候可使用他的默認配置;apache

    它可讓咱們搭建項目更快速,可無配置整合第三方框架;設計模式

    徹底不使用xml配置,只是用自動配置或是Java configtomcat

    它內嵌了servelt容器(tomcat、jetty),應用能夠用jar的方式運行springboot

    集成了應用運行狀態的監控app

三、學習springboot須要什麼知識儲備?框架

    java這個不用說了,哈哈maven

    maven這個必須會用啦

    spring 、spring MVC   這兩個是最基本的,這裏的spring 必須是 4.x版本,由於springboot使用了spring4.x 的@Conditional註解。

    想要學好它你還必須對設計模式有必定理解,還有core java中的 反射、註解、泛型 這些必不可少了,這部分有助於咱們去理解源碼。

第一個springboot demo

   一、 新建一個maven工程,你們能夠直接到spring官網上去新建一個,下載下來,或是本身建一個。我提倡手動。

    二、引入springboot依賴

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

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

完整的pom.xml

<?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>  
  
    <groupId>com.pxk</groupId>  
    <artifactId>springbootdemo </artifactId>  
    <version>0.0.1-SNAPSHOT</version>  
  
    <!-- Inherit defaults from Spring Boot -->  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>1.4.0.BUILD-SNAPSHOT</version>  
    </parent>  
  
    <!-- Add typical dependencies for a web application -->  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
    </dependencies>  
  
    <!-- Package as an executable jar -->  
    <build>  
        <plugins>  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
    <!-- Add Spring repositories -->  
    <!-- (you don't need this if you are using a .RELEASE version) -->  
    <repositories>  
        <repository>  
            <id>spring-snapshots</id>  
            <url>http://repo.spring.io/snapshot</url>  
            <snapshots><enabled>true</enabled></snapshots>  
        </repository>  
        <repository>  
            <id>spring-milestones</id>  
            <url>http://repo.spring.io/milestone</url>  
        </repository>  
    </repositories>  
    <pluginRepositories>  
        <pluginRepository>  
            <id>spring-snapshots</id>  
            <url>http://repo.spring.io/snapshot</url>  
        </pluginRepository>  
        <pluginRepository>  
            <id>spring-milestones</id>  
            <url>http://repo.spring.io/milestone</url>  
        </pluginRepository>  
    </pluginRepositories>  
</project>

    固然這是一個web工程,項目結構以下圖

 

Application.java

package com.pxk;

import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
	private static Logger logger = Logger.getLogger(Application.class);


	/**
	 * Main Start
	 */
	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
		logger.info("============= SpringBoot Start Success =============");
	}

}

HelloController1.java

package com.pxk;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController  
public class HelloController1 {
    @RequestMapping("/")  
    String home() {  
        return "Hello World pxk!";  
    }  
      
    @RequestMapping("/hello/{myName}")  
    String index(@PathVariable String myName) {  
        return "Hello "+myName+"!!!";  
    } 
}

而後直接run Application,springboot就會啓動內嵌的tomcat,默認是不帶項目名,且端口爲8080

訪問http://localhost:8080/ 以下圖

訪問:http://localhost:8080/hello/springboot,使用的是spring MVC的mapping方式,生成rest風格的訪問路徑

相關文章
相關標籤/搜索