Spring boot for Eclipse 開發指南第一節 快速建立

準備工做1java

java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)web

準備工做2spring

Eclipse Java EE IDE for Web Developers.apache

Version: Neon.1a Release (4.6.1)
Build id: 20161007-1200瀏覽器

該版本集成了MAVEN插件 無需安裝app

請配置好MAVEN的環境 (部分省略)webapp

 

搭建spring-boot 只需幾部 十分簡單maven

1. 新建一個3.0的 Web Pro0jectspring-boot

File - New - Dynamic Web Projectui

輸入 ProjectName 

Target Runtime 選擇 Apache Tomcat 8.5 若是沒有就 new 一個

Dynamic Web module version 選擇3.0 

Finish

2. 將工程轉換爲 Maven 工程

右鍵工程 Configure - Convert to Maven Project

在 GroupId 中輸入包名 例如 com.avicsafety.webapp

Finish

在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.avicsafety.webapp</groupId>
    <artifactId>MyWebApp</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.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    
    <build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<dependencies>
					<dependency>
						<groupId>org.springframework</groupId>
						<artifactId>springloaded</artifactId>
						<version>1.2.6.RELEASE</version>
					</dependency>
				</dependencies>
				<executions> 
                       <execution> 
                           <goals> 
                               <goal>repackage</goal> 
                           </goals> 
                           <configuration> 
                               <classifier>exec</classifier> 
                           </configuration> 
                       </execution> 
                    </executions>
			</plugin>
			<plugin>
			   <artifactId>maven-compiler-plugin</artifactId>
			   <configuration>
			      <source>1.8</source>
			      <target>1.8</target>
			   </configuration>
			</plugin>
		</plugins>
	</build>

</project>

右鍵工程 - Maven - Update Project 

轉換工程結束

 

3. 啓用工程

右鍵工程中的 src - Build Path - Use as Source Folder

展開工程中的 Java Respource 右鍵 src 選擇 New - Class

Package 中輸入本身的包名  com.avicsafety.webapp

Name 中輸入 MyApplication

Finish

在打開的MyApplication.java 中輸入如下代碼並保存

package com.avicsafety.webapp;

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;

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

右鍵該文件 選擇 Run As - Java Application 

控制檯出現 Started MyApplication in 3.096 seconds (JVM running for 3.449) 

恭喜 Spring boot 搭建完畢

 

打開瀏覽器輸入 http://localhost:8080/ 能夠看到 Hello World!

相關文章
相關標籤/搜索