您的位置:首页 > 编程语言 > Java开发

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

2016-08-22 17:57 721 查看
参考

https://spring.io/guides/gs/spring-boot/

首先下载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

都会显示相应的版本信息。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: