博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Springboot入门、配置文件介绍
阅读量:3960 次
发布时间:2019-05-24

本文共 2626 字,大约阅读时间需要 8 分钟。

Springboot入门

SpringBoot是什么?

Spring Boot它本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速、敏捷地开发新一代基于Spring框架的应用程序。也就是说,它并不是用来替代Spring的解决方案,而是和Spring框架紧密结合用于提升Spring开发者体验的工具。
同时它集成了大量常用的第三方库配置(例如Jackson, JDBC, Mongo, Redis, Mail等等),Spring Boot应用中这些第三方库几乎可以零配置的开箱即用(out-of-the-box),大部分的Spring Boot应用都只需要非常少量的配置代码,开发者能够更加专注于业务逻辑
注1:敏捷式开发
注2:spring boot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,spring boot整合了所有的框架

使用Idea配置SpringBoot项目

在这里插入图片描述

在这里插入图片描述
注意步骤二不能出现大写字母

在这里插入图片描述

在这里插入图片描述
注意第一次创建springboot比较缓慢
在这里插入图片描述
目录结构介绍
在这里插入图片描述
java源文件夹中的Springboot01Application.java是整个项目的启动类
static:存放的是静态资源的文件
templetes:存放的项目所需的页面
application.properties里面存放的是项目的全局配置信息
test:测试类
项目启动成功访问界面如下
在这里插入图片描述
在这里插入图片描述

测试案例代码

package com.zxp.springboot1.controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;import java.util.Map;/** * @author笑笑 * @site www.xiaoxiao.com * @company  * @create 2019-11-30 16:29 */@RestController@RequestMapping("/hello")public class HelloController {
@RequestMapping("/say1") public String say1(){
return "hello springboot 你大爷"; } @RequestMapping("/say2") public Map say2(){
Map map = new HashMap(); map.put("code",1); map.put("msg","jspn处理成功!!!"); return map; }}

测试say1

在这里插入图片描述
测试say2
在这里插入图片描述

Springboot配置文件

内置属性

在application.properties中配置
在这里插入图片描述
注意:实际项目开发的时候Port=80,Context-path=/,以下配置只为讲解相关知识点
代码

@RequestMapping("/say3")    public String say3(){
return "hello springboot"; }

浏览器访问结果

在这里插入图片描述

自定义属性

在application.properties中配置自定义属性如何在页面得到

方式一

在这里插入图片描述

@Value("${user.name}")    private  String name;    @Value("${user.pwd}")    private  String pwd;    @RequestMapping("/say4")    public Map say4(){
Map map = new HashMap(); map.put("name",name); map.put("pwd",pwd); return map; }

浏览器访问结果

在这里插入图片描述
属性封装类
定义属性封装类
方式二
在这里插入图片描述

package com.zxp.springboot1.entity;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;/** * @author笑笑 * @site www.xiaoxiao.com * @company * @create 2019-12-26 20:28 */@Component//标识为spring管理的主键@ConfigurationProperties(prefix = "mysql")//标识为属性封装类@Data//加上这个注解相当于set/get/tostringpublic class MysqlEntity {
private String driver; private String url; private String username; private String password;}

点击右上角,添加pom依赖,解决报红问题

org.springframework.boot
spring-boot-configuration-processor
true

添加controler层的Java代码进行测试

@Resource    private MysqlEntity mysqlEntity;    @RequestMapping("/say5")    public MysqlEntity say5(){
return mysqlEntity; }

浏览器访问结果

在这里插入图片描述

转载地址:http://aurzi.baihongyu.com/

你可能感兴趣的文章
linux数据库导出结果集且比对 && grep -v ---无法过滤的问题
查看>>
shell函数与自带变量
查看>>
linux下shell获取不到PID
查看>>
sort详解
查看>>
linux,shell中if else if的写法,if elif
查看>>
shell中单引号、双引号、反引号的区别
查看>>
shell脚本死循环方法
查看>>
shell中$*和$@的区别
查看>>
log4cxx 的编译安装过程和使用
查看>>
简单邮件系统程序
查看>>
STL里的multimap使用详解
查看>>
STL 库其中的 std::string用法总结
查看>>
模态对话框的销毁过程与非模态对话的几种销毁方法
查看>>
C++实现http下载 && 24点计算编码风格
查看>>
memcached了解使用和常用命令详解
查看>>
GDB调试各功能总结
查看>>
"undefined reference to" 多种可能出现的问题解决方法
查看>>
类结构定义
查看>>
Windows下关于多线程类 CSemaphore,CMutex,CCriticalSection,CEvent,信号量CSemaphore的使用介绍
查看>>
图像处理基本算法(汇总)以及实现
查看>>