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
这样启动项目那么访问的端口也就变成了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"}
|
转载于:https://www.cnblogs.com/gyadmin/p/9192327.html
springboot入门介绍相关推荐
- Springboot简单介绍
Springboot入门介绍 一.Spring框架概述 1.1 什么是Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnso ...
- SpringBoot 2.0 系列001 -- 入门介绍以及相关概念
为什么80%的码农都做不了架构师?>>> SpringBoot 2.0 系列001 -- 入门介绍以及相关概念 什么是SpringBoot? 项目地址:http://proje ...
- SpringBoot入门(二)——起步依赖
本文来自网易云社区 在前一篇我们通过简单几步操作就生成了一个可以直接运行的Web程序,这是因为SpringBoot代替我们做了许多工作,概括来讲可以分为起步依赖和自动配置.这一篇先来看看起步依赖. 项 ...
- 【Java】SpringBoot入门学习及基本使用
SpringBoot入门及基本使用 SpringBoot的介绍我就不多说了,核心的就是"约定大于配置",接下来直接上干货吧! 本文的实例: github-LPCloud,欢迎sta ...
- 最简单最详细的SpringBoot入门项目——HelloWorld
最详细的SpringBoot入门项目--HelloWorld 关于SpringBoot的介绍以及优点这里就不说了,想了解的可以直接点击这里 Spring Boot百度百科 接下来我们直奔主题,开始用S ...
- SpringBoot简介、SpringBoot 入门程序搭建、与JDBC、Druid、Mybatis和SpringData JPA的整合
一.SpringBoot 简介: spring boot并不是一个全新的框架,它不是spring解决方案的一个替代品,而是spring的一个封装.所以,你以前可以用spring做的事情,现在用spri ...
- springboot 历史版本文档_乘风破浪,SpringBoot入门
SpringBoot入门篇 前言 在普通的java项目中,大量的xml文件配置起来相当繁琐,导致了开发效率非常低下,整合第三方框架的配置可能会存在冲突问题导致部署效率低,打包方式是将项目打成一个war ...
- (原创)SpringBoot入门
本文章是SpringBoot入门的介绍在这里 我会尽量写一些细节性的东西,我用的是IDEA2016 Tomcat7 JDK1.8 Maven3.3.9 IDEA Tomcat JDK Maven ...
- SpringBoot入门(四)——自动配置
本文来自网易云社区 SpringBoot之所以能够快速构建项目,得益于它的2个新特性,一个是起步依赖前面已经介绍过,另外一个则是自动配置.起步依赖用于降低项目依赖的复杂度,自动配置负责减少人工配置的工 ...
最新文章
- html 音乐播放透明,HTML如何设置video为不透明背景
- VB.NET 在项目中添加App.config的配置
- CentOS7下安装Redis伪集群(基于Redis官方Cluster集群模式版本redis-5.0.10)
- 交互式计算机图形学总结:第六章 从顶点到片元
- 百度App Objective-C/Swift 组件化混编之路(二)- 工程化
- 使用lxml代替beautifulsoup
- C++中的static 成员变量的一些注意点
- 很多中小企业都在用的3款开源软件被曝多个漏洞
- linux设置activemq开机启动,Activemq(centos7)开机自启动服务
- SqlServer三种常用窗口函数
- 安装java环境好_一键安装Java环境的好工具 你用了吗
- IIS6上Ssl开发环境的搭建
- 全局唯一序列号生成器-支持分布式
- 2022化工自动化控制仪表复训题库及模拟考试
- Spark 提交yarn 虚拟内存不足解决方法
- 修改Window的hosts文件提示“该文件被其他程序占用,无法修改问题”解决方案
- 七牛云测试域名过期失效后の自救指南
- java 启动参数 别名,Spring boot 参数别名处理
- 【linux】运行run文件显示cannot execute binary file
- 英语常用的62个英语句型,学英语须掌握
热门文章
- ASP.NET的学习之asp.net整体运行机制
- 北航教授李帅:“VR+医疗”仿真系统及关键技术分享
- CALD:性能优于 SOTA 的 detection-specific 主动学习方法
- Seesaw Loss:一种面向长尾目标检测的平衡损失函数
- mysql数据库应用的权限层级_涂抹MySQL--第5章 MySQL数据库中的权限体系 - 5.3权限级别(1)...
- 最强!超越卷积、自注意力机制:强大的神经网络新算子involution
- YOLOv4改进版重磅问世!Yolov4原班人马重磅推出Scaled-YOLOv4!业界最佳
- 收藏 | 彻底搞懂机器学习中的正则化
- 本硕985非科班渣硕2020腾讯计算机视觉算法实习面经(已获Offer)
- opencv-4.5.3 + opencv_contrib-4.5.3 + vtk-9.0.3编译(全流程)