当前位置: 移动技术网 > IT编程>开发语言>Java > springboot配置文件(一)

springboot配置文件(一)

2019年12月09日  | 移动技术网IT编程  | 我要评论

一、yaml语法

1、基本语法

k 空格 v 表示一对键值对(必须有空格),以空格的缩进来控制层级关系,只要是左对齐的一列数据,都表示同一个层级。属性和值大小写敏感

1 server:
2     port: 8081
3     servlet-path: /hello
2、值的规则

① 字面量: 普通值
② 字符串默认不用加上单引号和双引号
③ 双引号不会转义字符串里面的特殊字符,也就是说如果字符串中有一个 \n 那么就会换行
④ 单引号会转义特殊字符,特殊字符最终只是一个普通字符串数据

3、对象的写法

比如有一个对象person和一个dog对象,如下:

public class person {
    private string username;
    private integer age;
    private boolean boss;
    private date birth;

    private map<string,object> maps;
    private list<object> lists;
    private dog dog;
  //get set tostring 方法
 }
 
public class dog {
    private string name;
    private integer age;
    //get set tostring 方法
}

application.yml配置文件

person:
  username: lwb
  age: 18
  boss: true
  birth: 2019/12/8
  maps: {name: wt, age: 16}
  lists: [lwb, wt]
  dog:
    name: 小狗
    age: 3

上面的代码就是给对象person 配置注入值的yaml写法

4、多种写法

yaml中,按照缩进来控制层级,但是对象、map、数组、list、set等也可以有另一种写法。

#对象行内写法
person: {username: zhangsan,age: 20}
#数组(list、set)
pets:
  ‐ cat
  ‐ dog
  ‐ pig
#或者
pets: [cat,dog,pig]

二、application.properties写法回顾实例

person.age=18
person.birth=2019/12/7
person.boss=true
person.maps.k1=wt
person.maps.k2=19
person.dog.name=小狗
person.dog.age=3
person.lists=a,b,c

三、配置文件注入

要将配置文件中的值注入到javabean对象在,需要确定导入

<dependency>
       <groupid>org.springframework.boot</groupid>
       <artifactid>spring-boot-configuration-processor</artifactid>
       <optional>true</optional>
</dependency>

最终的实验环境为:

<dependencies>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-web</artifactid>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-configuration-processor</artifactid>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter-test</artifactid>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>
    </build>

要想将配置文件中的值,自动注入到上面的对象person中,那么就需要将person交给spring来管理。因此需要在person类上加上两个spring的注解:

@configurationproperties(prefix = "person")
@component
public class person {
//如上面的属性
}

@configurationproperties:告诉springboot将本类中的所有属性和配置文件中相关的配置进行绑定,prefix = “person”:配置文件中哪个下面的所有属性进行一一映射。有了上面的配置,那么就可以在我们想获取person对象的地方自动注入person对象,并且初始值和配置文件中的配置一致

@service
public class personservice {
    @autowired
    private person person;
    public void getperson() {
        system.out.println(person);
    }
}

四、application.properties写法出现乱码

如二中,使用application.properties的写法所示,其实获取到的汉字可能是乱码,具体解决配置方式如下:

 并且在application.properties文件中加上如下的配置:

spring.http.encoding.charset=utf-8
spring.http.encoding.force=true
spring.http.encoding.enabled=true

五、使用@value来设置值

可以使用spring原生注解@value来注入值

@component
public class person {
    @value("${person.username}")
    private string username;
    @value("#{11 * 2}")
    private integer age;
    @value("true")
    private boolean boss;
    @value("${person.birth}")
    private date birth;
    private map<string,object> maps;
    private list<object> lists;
    private dog dog;
    //get set tostring
 }

 

 由上例子可以看见,所有的设置值都是简单类型的,如果给maps、lists、dog也加上注入值标签,会出现什么结果呢?

@component
public class person {
    @value("${person.username}")
    private string username;
    @value("#{11 * 2}")
    private integer age;
    @value("true")
    private boolean boss;
    @value("${person.birth}")
    private date birth;
    @value("${person.maps}")
    private map<string,object> maps;
    @value("${person.lists}")
    private list<object> lists;
    @value("${person.dog}")
    private dog dog;
    // get set tostring方法
}
java.lang.illegalstateexception: failed to load applicationcontext
    at org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontext(defaultcacheawarecontextloaderdelegate.java:132)
    at org.springframework.test.context.support.defaulttestcontext.getapplicationcontext(defaulttestcontext.java:123)
    at org.springframework.test.context.web.servlettestexecutionlistener.setuprequestcontextifnecessary(servlettestexecutionlistener.java:190)
    at org.springframework.test.context.web.servlettestexecutionlistener.preparetestinstance(servlettestexecutionlistener.java:132)
    at org.springframework.test.context.testcontextmanager.preparetestinstance(testcontextmanager.java:244)
    at org.springframework.test.context.junit4.springjunit4classrunner.createtest(springjunit4classrunner.java:227)
    at org.springframework.test.context.junit4.springjunit4classrunner$1.runreflectivecall(springjunit4classrunner.java:289)
    at org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12)
    at org.springframework.test.context.junit4.springjunit4classrunner.methodblock(springjunit4classrunner.java:291)
    at org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:246)
    at org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:97)
    at org.junit.runners.parentrunner$3.run(parentrunner.java:290)
    at org.junit.runners.parentrunner$1.schedule(parentrunner.java:71)
    at org.junit.runners.parentrunner.runchildren(parentrunner.java:288)
    at org.junit.runners.parentrunner.access$000(parentrunner.java:58)
    at org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268)
    at org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61)
    at org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:70)
    at org.junit.runners.parentrunner.run(parentrunner.java:363)
    at org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:190)
    at org.junit.runner.junitcore.run(junitcore.java:137)
    at com.intellij.junit4.junit4ideatestrunner.startrunnerwithargs(junit4ideatestrunner.java:68)
    at com.intellij.rt.execution.junit.ideatestrunner$repeater.startrunnerwithargs(ideatestrunner.java:47)
    at com.intellij.rt.execution.junit.junitstarter.preparestreamsandstart(junitstarter.java:242)
    at com.intellij.rt.execution.junit.junitstarter.main(junitstarter.java:70)
caused by: org.springframework.beans.factory.beancreationexception: error creating bean with name 'person': injection of autowired dependencies failed; nested exception is java.lang.illegalargumentexception: could not resolve placeholder 'person.maps' in value "${person.maps}"

可以看见直接报错,其实@configurationproperties和@value是有区别的,具体如下:

 @configurationproperties@value
功能 批量注入配置文件中的属性 一个个指定
松散绑定(松散语法)(lastname --> last-name) 支持 不支持
spel 不支持 支持
jsr303数据校验 支持 不支持
复杂类型封装 支持 不支持

 配置文件yml还是properties他们都能获取到值;如果说,我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@value;如果说,我们专门编写了一个javabean来和配置文件进行映射,我们就直接使用@configurationproperties;

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网