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中不存在的配置。

3.如何自定义读取配置文件中的配置

在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入门介绍的更多相关文章

  1. Springboot入门及配置文件介绍(内置属性、自定义属性、属性封装类)

    目的: 1.Springboot入门 SpringBoot是什么? 使用Idea配置SpringBoo使用t项目 测试案例 2.Springboot配置文件介绍 内置属性 自定义属性 属性封装类 Sp ...

  2. SpringBoot入门教程(二)CentOS部署SpringBoot项目从0到1

    在之前的博文<详解intellij idea搭建SpringBoot>介绍了idea搭建SpringBoot的详细过程, 并在<CentOS安装Tomcat>中介绍了Tomca ...

  3. SpringBoot入门基础

    目录 SpringBoot入门 (一) HelloWorld. 2 一 什么是springboot 1 二 入门实例... 1 SpringBoot入门 (二) 属性文件读取... 16 一 自定义属 ...

  4. 【Java】SpringBoot入门学习及基本使用

    SpringBoot入门及基本使用 SpringBoot的介绍我就不多说了,核心的就是"约定大于配置",接下来直接上干货吧! 本文的实例: github-LPCloud,欢迎sta ...

  5. SpringBoot入门(三)——入口类解析

    本文来自网易云社区 上一篇介绍了起步依赖,这篇我们先来看下SpringBoot项目是如何启动的. 入口类 再次观察工程的Maven配置文件,可以看到工程的默认打包方式是jar格式的. <pack ...

  6. SpringBoot入门(五)——自定义配置

    本文来自网易云社区 大部分比萨店也提供某种形式的自动配置.你可以点荤比萨.素比萨.香辣意大利比萨,或者是自动配置比萨中的极品--至尊比萨.在下单时,你并没有指定具体的辅料,你所点的比萨种类决定了所用的 ...

  7. SpringBoot入门(四)——自动配置

    本文来自网易云社区 SpringBoot之所以能够快速构建项目,得益于它的2个新特性,一个是起步依赖前面已经介绍过,另外一个则是自动配置.起步依赖用于降低项目依赖的复杂度,自动配置负责减少人工配置的工 ...

  8. SpringBoot入门(二)——起步依赖

    本文来自网易云社区 在前一篇我们通过简单几步操作就生成了一个可以直接运行的Web程序,这是因为SpringBoot代替我们做了许多工作,概括来讲可以分为起步依赖和自动配置.这一篇先来看看起步依赖. 项 ...

  9. SpringBoot入门(一)——开箱即用

    本文来自网易云社区 Spring Boot是什么 从根本上来讲Spring Boot就是一些库的集合,是一个基于"约定优于配置"的原则,快速搭建应用的框架.本质上依然Spring, ...

随机推荐

  1. 微信小程序 - 事件 | 传递 | 冒泡

    事件 常见的事件有: 类型 触发条件 最低版本 touchstart 手指触摸动作开始   touchmove 手指触摸后移动   touchcancel 手指触摸动作被打断,如来电提醒,弹窗   t ...

  2. [RN] React Native FlatList 选中后 状态没有立即发生改变,而在下一次生效的问题

    React Native FlatList 选中后 状态没有立即发生改变,而在下一次生效的问题 解决关键: 给 FlatList 添加 extraData={this.state} 非常关键,如果不设 ...

  3. Spring 中的异常处理

    工作中遇到这样的同事,问他活干完吗,他说开发好了,结果测试时发现各种异常情况未处理,联调测试时各种未知错误,最后联调完成比他预期的两倍工作量还多.这样的开发如果是新人还可以原谅,如果有工作经验且出现多 ...

  4. idea创建maven多模块Spring Boot项目

    1, 创建父项目 1.1,file - new - project 1.2,选择maven,Create from archetype(有的说不选,有的没说,不过我建父项目的时候没有勾选) 1.3,根 ...

  5. SpringBoot 2.x 整合Lombok

    Lombok的官方介绍 Project Lombok is a java library that automatically plugs into your editor and build too ...

  6. PDMan-2.1.0 正式发布:用心开源,免费的国产数据库建模工具 PowerDesigner

    PDMan是一款开源免费的数据库模型建模工具,支持Windows,Mac,Linux等操作系统,是PowerDesigner之外,更好的免费的替代方案.他具有颜值高,使用简单的特点.包含数据库建模,灵 ...

  7. Docker使用compose(原Fig)快速编配

    Docker使用compose(原Fig)快速编配 目录 安装 应用 构建以及运行 安装 在Linux上安装Fig: 在OS上安装: 在Linux上安装Fig: sudo bash-c "c ...

  8. Python OpenCV 显示图片,图片分类

    def divide_image(path,g_path1,g_path0): img_lst = os.listdir(path) for i in img_lst: print('类别1,类别0' ...

  9. mysql性能的检查和优化方法

    这个命令可以看到当前正在执行的sql语句,它会告知执行的sql.数据库名.执行的状态.来自的客户端ip.所使用的帐号.运行时间等信息 mysql在遇到严重性能问题时,一般都有这么几种可能:1.索引没有 ...

  10. office常用技巧汇总

    1.excel篇 (1)一次选择多行 可以利用SHIFT+鼠标实现,点第一行,按下鼠标,点200行,就能实现1~200行选择了. 总结:就是一直按住shift键,鼠标点击要选择的首行,再点击尾行.