springboot的Demo

概述

约定大于配置
spring缺点
1.配置繁琐(一大堆的xml
2.依赖繁琐(maven的各种dependency
针对这两点
springboot提供了以下两个
1.自动配置
2.起步依赖(传递依赖)
3.辅助功能(嵌入服务器等
总结:springboot并不是spring的增强,而是提供了一种快速使用spring的方式

springboot快速入门

创建maven工程
导入springboot起步依赖
定义controller
编写引导类
springboot在创建项目的时候,使用jar打包方式
springboot的引导类,是项目的入口,运行main方法就可以启动项目
springboot和spring构建的项目,业务代码编写是一样的

起步依赖

spring-boot-starter-parent定义了各种技术的版本信息,组合了一套最优
starter(例如spring-boot-starter-web)定义了完成该功能所需的坐标合集,其中大部分版本信息来自父工程
我们的工程继承parent,引入starter后,通过依赖传递,就可以方便的获取各种jar包了,并且不存在版本冲突问题。

配置文件

配置文件两种:properties,yaml文件
默认配置文件名叫application
同一级目录下优先级:.properties>.yml>.yaml

yaml

简洁,以数据为核心。相比properties和xml更有优势
值之前空格,缩进,大小写敏感

#对象,数组,纯量,参数引用
name: abc
#对象
person:
  name: ${name} #参数引用
  age: 26
person2: {name: liu, age: 26}
#数组
address:
  - beijing
  - shanghai
address1: [beijing,shanghai]
#纯量
msg1: 'hello \n world' # 不能识别换行\n
msg2: "hello \n world" # 可以识别换行\n

读取数据

三种方式@Value,Environment,@ConfigurationProperties

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

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

    @Value("${address[0]}")
    private String address1;

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

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

    @Autowired
    private Environment env;
    
	System.out.println(env.getProperty("person.age"));
	System.out.println(env.getProperty("address[0]"));
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    private String name;
    private int age;
    private String[] address;
}

profile

开发,测试,生产环境
动态配置切换的

profile配置方式

1.多profile文件方式
在这里插入图片描述
2.yaml多文档方式
在这里插入图片描述
配置文件
虚拟机参数(-D开头)
命令行参数(–开头)
在这里插入图片描述
选择maven的package打包
在这里插入图片描述

Building jar: E:\JavaCode\day25_springboot_helloword\day26_springboot_init\target\day26_springboot_init-0.0.1-SNAPSHOT.jar

在target目录下有相应的jar包,运行即可
在这里插入图片描述

项目内部配置文件加载顺序

springboot程序启动时,会从以下位置加载配置文件
1.file:./config/当前项目下的/config目录下(是项目,不是模块哦
2.file:./当前项目的根目录
3.classpath:/config/:classpath的/config目录(classpath就是resources
4.classpath::/classpath的根目录

项目外部配置文件加载顺序

1.命令行
2.命令行指定文件–spring.config.location
3.放到发布后的target根目录下或者target的config目录下,启动命令不用修改

springboot整合junit

搭建springboot项目
引入starter-test起步依赖
编写测试类
添加测试相关注解
@RunWith(SpringRunner.class)(可能版本问题会报错
@SpringBootTest(classes=启动类.class)
编写测试方法

注意包扫描问题

springboot整合redis

搭建springboot项目
引入redis起步依赖
配置redis相关属性
注入RedisTemplate模板
编写测试方法

springboot整合mybatis

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值