springboot入门介绍
1.
SpringBoot学习之@SpringBootApplication注解
下面是我们经常见到SpringBoot启动类代码:
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
这里主要关注@SpringBootApplication注解,它包括三个注解:
@Configuration:表示将该类作用springboot配置文件类。
@EnableAutoConfiguration:表示程序启动时,自动加载springboot默认的配置。
@ComponentScan:表示程序启动是,自动扫描当前包及子包下所有类。
自动加载springboot默认的配置介绍
SpringBoot学习<二>——SpringBoot的默认配置文件application和多环境配置
一、SpringBoot的默认文件appliction
上一篇文章已经说明,springboot启动会内嵌tomcat,端口也是默认的8080,如果我们想要改变端口如果做呢?
在springboot项目中会有一个默认的配置文件appliction,在类路径下,后缀有两种,一种是常见的properties,另一种是spring官方推荐使用的yaml格式,因为本人习惯于使用properties的,所以yml不做介绍,只是有一些书写格式的区别,并无太大差别。回到上面,想要修改端口的配置,只需在application.properties文件里,写上server.port=8010即可,
server.port=
这样启动项目那么访问的端口也就变成了8010,当然,他不仅仅只限于配置这么一些,springboot基本整合了很多配置,我们需要配置自己的个性化设置通常只需在此配置文件中写入响应的配置即可,包括数据源,redis等等,而此规范,spring提供文档,大家需要什么配置只需参考spring提供的文档即可。
https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#using-boot
当然,在实际开发中,我们可能会有一些自己的配置i,我们可以通过@PropertySource注解读取文件 ,不过不支持yaml的文件。
在此配置文件中也是支持占位符的,如下:
sam.one=com.sam
sam.tow=${sam.one}.springboot
二、多环境配置
springboot还提供一种多环境配置,然你的配置可以在开发,生成,测试中自由切换,减少了不必要的错误。
一般都是在类路径下,新建三个properties文件,application-test , application-pro, application-dev,然后在核心配置application中如下配置
spring.profiles.active=test
代码中指定是的test测试环境下,这样就实现了springboot的多环境配置,springboot会优先去选择加载选择环境中的配置,然后才会去加载这样环境中在application中不存在的配置。
在spring boot中,简单几步,读取配置文件(application.yml)中各种不同类型的属性值:
1、引入依赖:
|
1
2
3
4
5
6
|
<!-- 支持 @ConfigurationProperties 注解 --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> |
2、配置文件(application.yml)中配置各个属性的值:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
myProps: #自定义的属性和值 simpleProp: simplePropValue arrayProps: 1,2,3,4,5 listProp1: - name: abc value: abcValue - name: efg value: efgValue listProp2: - config2Value1 - config2Vavlue2 mapProps: key1: value1 key2: value2 |
3、创建一个bean来接收配置信息:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
@Component@ConfigurationProperties(prefix="myProps") //接收application.yml中的myProps下面的属性 public class MyProps { private String simpleProp; private String[] arrayProps; private List<Map<String, String>> listProp1 = new ArrayList<>(); //接收prop1里面的属性值 private List<String> listProp2 = new ArrayList<>(); //接收prop2里面的属性值 private Map<String, String> mapProps = new HashMap<>(); //接收prop1里面的属性值 public String getSimpleProp() { return simpleProp; } //String类型的一定需要setter来接收属性值;maps, collections, 和 arrays 不需要 public void setSimpleProp(String simpleProp) { this.simpleProp = simpleProp; } public List<Map<String, String>> getListProp1() { return listProp1; } public List<String> getListProp2() { return listProp2; } public String[] getArrayProps() { return arrayProps; } public void setArrayProps(String[] arrayProps) { this.arrayProps = arrayProps; } public Map<String, String> getMapProps() { return mapProps; } public void setMapProps(Map<String, String> mapProps) { this.mapProps = mapProps; } } |
启动后,这个bean里面的属性就会自动接收配置的值了。
4、单元测试用例:
|
1
2
3
4
5
6
7
8
9
10
11
|
@Autowired private MyProps myProps; @Test public void propsTest() throws JsonProcessingException { System.out.println("simpleProp: " + myProps.getSimpleProp()); System.out.println("arrayProps: " + objectMapper.writeValueAsString(myProps.getArrayProps())); System.out.println("listProp1: " + objectMapper.writeValueAsString(myProps.getListProp1())); System.out.println("listProp2: " + objectMapper.writeValueAsString(myProps.getListProp2())); System.out.println("mapProps: " + objectMapper.writeValueAsString(myProps.getMapProps())); } |
测试结果:
|
1
2
3
4
5
|
simpleProp: simplePropValue arrayProps: ["1","2","3","4","5"] listProp1: [{"name":"abc","value":"abcValue"},{"name":"efg","value":"efgValue"}] listProp2: ["config2Value1","config2Vavlue2"] mapProps: {"key1":"value1","key2":"value2"} |
springboot入门介绍的更多相关文章
- Springboot入门及配置文件介绍(内置属性、自定义属性、属性封装类)
目的: 1.Springboot入门 SpringBoot是什么? 使用Idea配置SpringBoo使用t项目 测试案例 2.Springboot配置文件介绍 内置属性 自定义属性 属性封装类 Sp ...
- SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1
在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...
- SpringBoot入门基础
目录 SpringBoot入门 (一) HelloWorld. 2 一 什么是springboot 1 二 入门实例... 1 SpringBoot入门 (二) 属性文件读取... 16 一 自定义属 ...
- 【Java】SpringBoot入门学习及基本使用
SpringBoot入门及基本使用 SpringBoot的介绍我就不多说了,核心的就是"约定大于配置",接下来直接上干货吧! 本文的实例: github-LPCloud,欢迎sta ...
- SpringBoot入门(三)——入口类解析
本文来自网易云社区 上一篇介绍了起步依赖,这篇我们先来看下SpringBoot项目是如何启动的. 入口类 再次观察工程的Maven配置文件,可以看到工程的默认打包方式是jar格式的. <pack ...
- SpringBoot入门(五)——自定义配置
本文来自网易云社区 大部分比萨店也提供某种形式的自动配置.你可以点荤比萨.素比萨.香辣意大利比萨,或者是自动配置比萨中的极品--至尊比萨.在下单时,你并没有指定具体的辅料,你所点的比萨种类决定了所用的 ...
- SpringBoot入门(四)——自动配置
本文来自网易云社区 SpringBoot之所以能够快速构建项目,得益于它的2个新特性,一个是起步依赖前面已经介绍过,另外一个则是自动配置.起步依赖用于降低项目依赖的复杂度,自动配置负责减少人工配置的工 ...
- SpringBoot入门(二)——起步依赖
本文来自网易云社区 在前一篇我们通过简单几步操作就生成了一个可以直接运行的Web程序,这是因为SpringBoot代替我们做了许多工作,概括来讲可以分为起步依赖和自动配置.这一篇先来看看起步依赖. 项 ...
- SpringBoot入门(一)——开箱即用
本文来自网易云社区 Spring Boot是什么 从根本上来讲Spring Boot就是一些库的集合,是一个基于"约定优于配置"的原则,快速搭建应用的框架.本质上依然Spring, ...
随机推荐
- jenkins slave上执行脚本报错
jenkins slave上执行脚本报错 解决方法:在系统配置中设置shell execuate C:\Windows\system32\cmd.exe 保存即可
- Nginx配置文件nginx.conf(八)
原文链接:https://www.cnblogs.com/knowledgesea/p/5175711.html 在nginx.conf的注释符号是#. 默认的nginx.conf内容为: #user ...
- 生产器&迭代器
生成器 列表生成器:简洁代码 >>> a = [i+1 for i in range(10)] >>> a [1, 2, 3, 4, 5, 6, 7, 8, 9, ...
- 怎么删除STL容器的元素
在STL容器有顺序容器和关联容器两种. 顺序容器删除元素的方法有两种: 1.c.erase(p) 从c中删除迭代器p指定的元素.p必须指向c中一个真实元素,不能等于c.end().返回一个指向p之后元 ...
- [LeetCode] 466. Count The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- Spring Boot中的Mongodb多数据源扩展
在日常工作中,我们通过Spring Data Mongodb来操作Mongodb数据库,在Spring Boot中只需要引入spring-boot-starter-data-mongodb即可. 然后 ...
- 搭建Jena Fuseki并执行SPARQL查询
1. 下载Jena Fuseki:http://jena.apache.org/download/index.cgi 2. 运行服务 windows解压后双击fuseki-server.bat lin ...
- NetCore 开发时中文编码转换出现异常
在C#编程的时候难免会遇到需要转换编码的场合. 在Framwork中可以用System.Text.Encoding解决,但是到了core会发现,虽然也有这个东西,但几个关键的中文编码(比如GB2312 ...
- [Powershell]发布基于.NET Framework的WebAPI和Job控制台程序项目
获取要发布的定时计划任务. 禁用和停止定时计划任务. 批量强制结束Job进程. 打印定时计划任务状态. 备份项目文件夹. 发布项目文件夹. 删除部署包. 启用定时计划任务. <# .NOTES ...
- CentOS7 GitLab 安装
1.安装依赖 $ yum -y install policycoreutils openssh-server openssh-clients postfix $ yum install policyc ...