第一章:SpringBoot入門

源碼下載:https://u11556602.ctfile.com/fs/11556602-361219278java

                https://download.csdn.net/download/qq_36267875/11089023web

1.建立maven項目spring

2.若是要想開發springboot程序只須要按照官方給出的要求配置一個父pom便可。apache

將下面的配置拷貝到pom中springboot

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

就像下面這樣 pomapp

<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.mldn</groupId>
  <artifactId>bootfirst</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>bootfirst</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

而後發現junit的版本顯示黃色感嘆號,這是由於,咱們引入了的parent裏面規定了版本號,因此這裏的版本號就能夠去掉了maven

設置jdk版本spring-boot

在<properties>中設置版本號測試

<properties>
  	<jdk.version>1.8</jdk.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

設置插件ui

<build>
		<finalName>bootfirst</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>${jdk.version}</source><!-- 源代碼使用的開發版本 -->
					<target>${jdk.version}</target><!-- 須要生成的目標class文件的編譯版本 -->
					<encode>${project.build.sourceEncoding}</encode>
				</configuration>
			</plugin>
		</plugins>
	</build>

而後更新maven項目,發現版本變成了1.8

配置springboot依賴程序

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

以上pom文件的配置就完成了,完整的pom文件以下

<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.mldn</groupId>
  <artifactId>bootfirst</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>bootfirst</name>
  <url>http://maven.apache.org</url>

  <properties>
  	<jdk.version>1.8</jdk.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>    
  </dependencies>
  
	<build>
		<finalName>bootfirst</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>${jdk.version}</source><!-- 源代碼使用的開發版本 -->
					<target>${jdk.version}</target><!-- 須要生成的目標class文件的編譯版本 -->
					<encode>${project.build.sourceEncoding}</encode>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

3.編寫一個具體的程序

 

package com.mldn.bootfirst;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class SampleController {
	@RequestMapping("/")
	@ResponseBody
	public String home() {
		return "Hello World!";
	}
	public static void main(String[] args) throws Exception {
		SpringApplication.run(SampleController.class, args);
	}
}

而後能夠直接經過java去運行該springboot程序。

在SampleController.java文件中,右鍵 Run as --java application

訪問測試 http://localhost:8080/

若是如今是一個maven的普通項目,最簡單的方法是在maven運行的時候輸入:"spring-boot:run" 

在項目上點擊右鍵,run as -- maven build 選擇第四個

開始下載插件,等插件下載完成以後,就能夠執行了,就和編譯打包是同樣的

springboot的天生缺陷:是對開發者的要求比較高,必須會maven,ssm整合開發。

相關文章
相關標籤/搜索