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

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"}
原文地址:https://www.cnblogs.com/gyadmin/p/9192327.html