Spring Boot入门一:在Eclipse中使用Spring boot

Eclipse中的STS插件

  • 打开Eclipse-Help-Eclipse Marketplace-popularSpring Boot入门一:在Eclipse中使用Spring boot_第1张图片
  • 下载完成后,重启eclipse,选择新建project-spring-spring start project,新建Spring boot 项目。
    Spring Boot入门一:在Eclipse中使用Spring boot_第2张图片

项目快速解析

  • 新建的项目目录大概如下
    Spring Boot入门一:在Eclipse中使用Spring boot_第3张图片
  • 整体目录结构和Spring项目一样,只不过Spring boot的配置文件迁移到了application.yml(或者application.propertis)。项目的入口为BootTestApplication.java的main函数入口。
  • 在maven配置的pom.xml里面相较于之前的项目添加了以下依赖:
    Spring Boot入门一:在Eclipse中使用Spring boot_第4张图片
    Spring Boot入门一:在Eclipse中使用Spring boot_第5张图片
  • BootTestApplication中@SpringBootApplication取代了Spring项目中的@Configuration、@EnableAutoConfiguration、@ComponentScan。因此,我们所有的bean需要在BootTestApplication的同级目录下被扫描。
    Spring Boot入门一:在Eclipse中使用Spring boot_第6张图片

快速开发项目

  • 在pom.xml引入依赖
<dependency><groupId>org.springframework.bootgroupId>  
<artifactId>spring-boot-starter-webartifactId>  
dependency>
  • 在com.example.demo下新建controller包,新建controller包,在下面建立Hello Controller.java。
@RestController  
public class HelloController {  

    @RequestMapping(value="/hello", method=RequestMethod.GET)  
    public String HelloController() {  
        return "Hello World!";  
    }  

}  
  • 右键BootTestApplication.java-run as java application,运行项目。
  • 在浏览器输入localhost:8080/hello。
    Spring Boot入门一:在Eclipse中使用Spring boot_第7张图片

修改访问端口和默认路径

  • 在这里我们使用更加好看的application.yml方式配置。只需要将默认的application.properties替换为application.yml即可。
  • 在yml添加以下代码,修改端口为8081,后缀添加路径为/guo
server:
  port: 8081
  context-path: /guo
  • 打开浏览器,输入http://localhost:8081/guo/hello
    Spring Boot入门一:在Eclipse中使用Spring boot_第8张图片

你可能感兴趣的:(spring,Spring,Boot)