要在Eclipse里使用SpringBoot,首先要安装下STS插件。安装插件之前,我建议配置下Eclipse里的Maven插件,改成国内阿里云镜像:
在Settings.xml文件里加上镜像
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
后面新建springboot项目的时候,会下载jar包,用国内镜像会快很多。
现在就可以安装STS插件了,我建议使用1.8jdk,否则可能会出现安装失败的情况。
打开Eclipse 菜单栏 Help -> Eclipse Marketplace...
选择Popular,然后下载。
等待安装
选择yes重启生效
现在就安装成功插件了,如果安装失败就多安装几次,我也是安装了三次才成功。
我们来新建一个SpringBoot项目。新建 选择 Spring->Spring Starter Project
这里修改一下
点击下一步
我们暂时选择一个Web依赖即可,这里也可以选择Spring Boot的版本,点击finish,这时候会下载很多Jar包。
生成的项目结构:
package com.wya.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootPropertiesApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootPropertiesApplication.class, args);
}
}
HelloApplication是启动类。
1. @SpringBootApplication:Spring Boot 应用的标识
2. Application很简单,一个main函数作为主入口。SpringApplication引导应用,并将Application本身作为参数传递给run方法。具体run方法会启动嵌入式的Tomcat并初始化Spring环境及其各Spring组件。
3.@SpringBootApplication的源代码中有个ComponentScan注解,它会扫描该类所在的包和子包,所以建议把启动类放到包的根包下。
我们右击Run As 选择 Spring Boot app
这里我们不需要Tomcat 因为SpringBoot把Tomcat集成了,默认端口8080,默认路径是根路径。
application.properties是配置文件。
控制台输出:
浏览器访问:http://localhost:8080/
说明项目启动成功,Error是因为什么都没有配置。
现在配置一个HelloController
package com.wya.springboot.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("hello")
public String hello(){
return "你好 Spring Boot";
}
}
重新启动项目
请求http://localhost:8080/hello
至此一个Spring Boot 的helloWorld实现就成功了。