springboot入门helloworld

一、创建springboot项目
1、使用intellij idea创建springboot项目
在这里插入图片描述
输入项目名称
在这里插入图片描述
选中Web选项
在这里插入图片描述
输入项目名称和项目保存路径
在这里插入图片描述
项目目录结构如下
在这里插入图片描述
至此项目创建完成,接下来讲解如何开发;
二、springboot的helloworld编写
1、项目application.yml文件配置
在这里插入图片描述
配置文件有三个,分别为
application.yml:配置文件入口,可以指定启用开发环境配置文件还是生产环境配置文件,内如如下,以下配置启用了生产环境配置文件;

spring:
  profiles:
    active: pro

application-dev.yml:开发环境配置文件,内容如下,
配置了项目访问的上下文路径 /demo,项目的访问端口8081,还有项目中一些特有项目配置设置;

server:
    servlet:
        context-path: /demo
    port: 8081
name: dev
age: 11

application-pro.yml:生产环境配置,配置项和开发环境一致,只是根据生产环境配置值的不同做下修改即可;
2、创建控制器类HelloController
创建类
在这里插入图片描述
编码内容为

@RestController
public class HelloController {

    @Value("${name}")
    private String name;

    @Value("${age}")
    private String age;

    private HelloService helloService;

    @Autowired
    public void setHelloService(HelloService helloService) {
        this.helloService = (HelloService) CustomBeanFactory.getBean(helloService);
    }

    @RequestMapping("/hello")
    public String say(){
        return "我是"+name+",我今年"+age;
    }
}

3、启动springboot服务
启动类:HelloworldApplication,右键运行即可
在这里插入图片描述
4、在浏览器访问
在这里插入图片描述
至此一个springboot的helloworld写完了;
springboot官网:https://spring.io/projects/spring-boot
springboot源码:https://github.com/spring-projects/spring-boot
总结:springboot大大简化了我们的web开发,减去了繁杂的配置,提升了开发效率;让我们可以更专注的去做业务的开发;

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章