springBoot入门第一篇——搭建简单项目

大家都在用springboot,从入门开始学习咯。

一篇一篇记录自己的学习,希望自己能坚持下去。

先从简单的项目搭建开始,SpringBoot的项目搭建非常的简单便捷,不像spring那样需要自己手动配置那么多的依赖。

一下是pom.xml 文件,配置 parent后则不需要像spring那样再去配置那么多的东西,springboot会帮我们导入。


    
    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.6.RELEASE
    

    

        
        
            org.springframework.boot
            spring-boot-starter-web
            1.5.6.RELEASE
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    

创建一个boot1Application 类作为项目启动的入口:

SprinBootApplication 注解是 Springboot的核心注解之一,通过使用该注解,声明该类为项目启动类。在其 main 方法中,调用SpringApplication.run(class)  方法启动项目。

@Value 注解,会从配置文件中获取对应的属性值进行赋值

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class boot1Application {

    @Value("${project.creator}")
    String creator;
    @Value("${project.time}")
    String time;

    @RequestMapping("/")
    public String index(){
        return "hello world! " + creator + time;
    }

    public static void main(String args[]){

        SpringApplication.run(boot1Application.class);
    }
}

 

接着在resource文件夹下的application.properties 配置文件中配置服务器端口以及映射地址,还可以配置属性,以下是简单的配置:

server.port=8090
server.context-path=/boot1

#添加属性
project.creator=luzi
project.time=2018/08/02

springboot 同时还支持 YAML 语言,所以还可以使用application.yml 配置文件代替applicaiton.properties。

server:
  port: 8080
  contextPath: /boot1
  
project:
 creator: luzi
 time: 2018/08/02

两者是等价的。当项目中同时存在 application.properties 和 application.yml 文件时,前者的优先级更高,也就是说相同的配置下 .properties 会覆盖掉 .yml 的配置。

到此 一个简单的springboot 项目就完成了。启动项目,在浏览器中输入 http://localhost:8090/boot1/,结果如下:

springBoot入门第一篇——搭建简单项目_第1张图片

你可能感兴趣的:(springboot入门)