第二章:springboot配置入门

版权声明:如需转载,请备注出处 https://blog.csdn.net/u013783065/article/details/81060255

springboot让您尽可能快速的启动和运行您的项目,在大多数情况下,并不需要我们进行太多的配置就可以完美的让springboot正常运行。然而往往,在很多时候,springboot的配置并不能满足于我们的需求,所以此时我们就需要进行一些配置的修改,并且加上我们自己的配置项。

自定义属性

当我们创建一个springboot项目的时候,系统会默认的为我们在src/main/java/resources目录下创建一个application.properties文件。但是通常情况下,我个人都会将其后缀改为yml。我们还是继续沿用上一个工程。

在配置文件中加如下内容。

server:
  port: 80  //修改端口号
hello:
  name: this is my first springboot        //自定义的内容

修改helloController类如下:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Value("${hello.name}")
    String helloName;
    @RequestMapping("hello/{name}")
    public String hello(@PathVariable("name") String name){
        return name+":"+helloName;
    }
}

运行项目SpringbootApplication的main方法:访问http://localhost/hello/world页面就会显示如图的内容

其中world是我们的访问路径作为了请求参数带过去的,this is my first springboot是我们在配置文件中所带内容!访问地址与上期相比,我们不需要再输入8080端口。

 

将配置文件的属性值赋给实体类

1、我们需要配置很多属性的时候,这时,我们会把这些属性作为字段来创建一个javabean,并将属性赋值给他们:

hello:
  name: this is my first springboot
  age: 12
  number: ${random.int}
  uuid: ${random.uuid}
  max:  ${random.int(10)}
  value:  ${random.value}
  greeting: hi,my name is ${hello.name}

在此处我们用到了,${random},它是用来生成各种不同类型的随机值

2、创建一个javabean

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "hello")
public class ConfigBean {
    private String name;
    private int age;
    private int number;
    private String uuid;
    private int max;
    private String value;
    private String greeting;//省略getter setter...

3、在application类加上@EnableConfigurationProperties({ConfigBean.class})注解

4、在hellocontroller新建一个方法

@Autowired
    ConfigBean configBean;

    @RequestMapping("java/bean")
    public ConfigBean configBean(){
        return configBean;
    }

5、重新运行程序,访问http://localhost/java/bean即可得到如下数据

{
    "name": "this is my first springboot",
    "age": 12,
    "number": -888717841,
    "uuid": "1bb11a38-5d9e-4a54-b298-59a5c9a57b20",
    "max": 7,
    "value": "5521d717ccc99e25f0305a4f6c940e88",
    "greeting": "hi,my name is this is my first springboot"
}

得到如下数据,即可证明我们的已经从我们的配置文件中读取到了数据,并且赋值成功。

自定义配置文件

在上面我们都是写在自带的配置文件中,当我们要做一些全局属性的时候,写在自带的配置文件中效果较好,除了一些全局的属性外,我们还是比较多使用一些自定义的配置文件,那我们又该怎么实现和上面相同的东西呢?

1、新建一个配置文件config.yml

2、将上面使用的配置项赋值到新建的配置文件中

3、在我们的实体类上添加一些注解

  •    @Configuration
  •    @PropertySource(value = "classpath:config.yml")//在1.4版本需要PropertySource加上location
  •    @ConfigurationProperties(prefix = "config-hello")

4、重新运行项目,并且访问http://localhost/java/bean即可得到如下数据

此处注意,我返回这个javabean对象时会报错,暂时还没有解决,也希望大神能够给予一些指导和帮助。

错误如下:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.context.expression.StandardBeanExpressionResolver and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: com.wujie.springboot.ConfigBean$$EnhancerBySpringCGLIB$$e7966309["$$beanFactory"]->org.springframework.beans.factory.support.DefaultListableBeanFactory["beanExpressionResolver"])

多环境配置文件

在我们实际的额开发环境中,我们使用的不仅仅是一个环境下的配置文件,我们通常拥有,开发、测试、生产三个环境

  • application-dev.properties
  • application-test.properties
  • application-pro.properties

配置好三个文件后,我们只需要在application.yml中加:

spring:
  profiles:
    active: dev

参考资料

SpringBoot非官方教程

欢迎关注我的公众号我们一起学习:

猜你喜欢

转载自blog.csdn.net/u013783065/article/details/81060255