本文共 2626 字,大约阅读时间需要 8 分钟。
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整合了所有的框架测试案例代码
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内置属性
在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/