1. 程式人生 > >Intellij和SpringBoot,gradle構建Hello world!工程

Intellij和SpringBoot,gradle構建Hello world!工程

參考

首先下載Intellij,然後

(1)新建一個工程:




package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@SpringBootApplication
public class DaoTestApplication {

	public static void main(String[] args) {
		ApplicationContext ctx = SpringApplication.run(DaoTestApplication.class, args);

		System.out.println("Let's inspect the beans provided by Spring Boot:");

		String[] beanNames = ctx.getBeanDefinitionNames();
		Arrays.sort(beanNames);
		for (String beanName : beanNames) {
			System.out.println(beanName);
		}
	}
}


package com.example;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "Hello world!";
    }

}


buildscript {
	ext {
		springBootVersion = '1.4.0.RELEASE'
	}
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
	}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'

jar {
	baseName = 'daotest'
	version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
	mavenCentral()
}


dependencies {
	// tag::jetty[]
	compile("org.springframework.boot:spring-boot-starter-web") {
		exclude module: "spring-boot-starter-tomcat"
	}
	compile("org.springframework.boot:spring-boot-starter-jetty")
	// end::jetty[]
	// tag::actuator[]
	compile("org.springframework.boot:spring-boot-starter-actuator")
	// end::actuator[]
	testCompile("junit:junit")
	testCompile('org.springframework.boot:spring-boot-starter-test')

	task wrapper(type: Wrapper) {
		gradleVersion = '2.3'
	}
}


eclipse {
	classpath {
		 containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
		 containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
	}
}


編譯執行:




注意:需要設定JDK8,這個地方我設定少了,卡了蠻久的。

Java JDK 8 在 Windows 8.1下的安裝以及環境變數的配置

在Windows 中,雙擊安裝就是。

Win8.1下JDK8環境變數的配置:

依次單擊計算機(Computer),選擇屬性(Properties),選擇高階系統設定(Advanced systems settings), 選擇環境變數(Environment  Variables).

 
新建3個環境變數(PATH,CLASSPATH, JAVA_HOME),若有則不用新建。
給3個環境變數增加相應的值(由Java所在的路徑決定,根據具體情況修改),例如:

PATH    D:\Program Files\Java\jdk1.8.0\bin;  D:\Program  Files\Java\jdk1.8.0\jre\bin
CLASSPATH  D:\Program  Files\Java\jdk1.8.0\lib;  D:\Program  Files\Java\jdk1.8.0\lib\tools.jar
JAVA_HOME    D:\Program  Files\Java\jdk1.8.0

不同路徑之間用分號隔開。

若新增正確,登出或重啟計算機以後,在PowerShell或cmd中輸入:

java   -version

javac   -version

都會顯示相應的版本資訊。