美文网首页
SpringBoot配置文件详解

SpringBoot配置文件详解

作者: 境里婆娑 | 来源:发表于2018-03-07 14:08 被阅读0次

    一、自定义属性

    当我们创建一个springboot项目的时候,系统默认会为我们在src/main/java/resources目录下创建一个application.properties。其中也支持application.yml。

    在application.yml自定义一组属性

    my:

     name: zhangsan

    age: 12

    如果你需要读取配置文件的值只需要加@Value("${}")

    @RestController

    public class MyController {

        @Value("${my.name}")

        private String name;

        @Value("${my.age}")

        private String age;

    }

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

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

    my:

    name: forezp

    age: 12 

    number:  ${random.int} 

    uuid : ${random.uuid} 

    max: ${random.int(10)} 

    value: ${random.value} 

    greeting: hi,i'm  ${my.name}

    其中配置文件中用到了${random} ,它可以用来生成各种不同类型的随机值。

    怎么讲这些属性赋于给一个javabean 呢,首先创建一个javabean :

    @ConfigurationProperties(prefix = "my")

    @Component

    public class ConfigBean {

        private String name;

       private int age;

        private int number;

        private String uuid;

        private int max;

        private String value;

        private String greeting;

    }

    需要加个注解@ConfigurationProperties,并加上它的prefix.另外@Component可加可不加。另外spring-boot-configuration-processor依赖可加可不加

    另外需要在应用类或者application类,加EnableConfigurationProperties注解

    @RestController

    @EnableConfigurationProperties({ConfigBean.class})

    public class LucyController {

        @Autowired

        ConfigBean configBean;

        @RequestMapping(value="/lucy")

        public String miya() {

            return configBean.getGreeting()+">>>"+configBean.getName();

        }

    }

    启动工程,访问localhost:8080/lucy,我们会发现配置文件信息读到了

    三、自定义配置文件

    上面介绍的是我们都把配置文件写到application.yml中。有时我们不愿意把配置都写到application配置文件中,这时需要我们自定义配置文件,比如test.properties:

    com.forezp.name = forezp

    com.forezp.age = 12

    怎么将这个配置文件信息赋予给一个javabena呢

    @Configuration

    @PropertySource(value = "classpath:test.properties")

    @ConfigurationProperties(prefix = "com.forezp")

    public class User {

        private String name;

        private int age;

    }

    最新版本的springboot,需要加这三个注解。@Configuration   @PropertySource(value = "classpath:test.properties")

    @ConfigurationProperties(prefix = "com.forezp");在1.4版本中需要PropertySource加上location。

    @RestController

    @EnableConfigurationProperties({ConfigBean.class,User.class})

    public class LucyController {

        @Autowired

        ConfigBean configBean;

        @RequestMapping(value="/lucy")

        public String miya() {

            return configBean.getGreeting()+">>>"+configBean.getName();

        }

        @Autowired

        User user;

        @RequestMapping(value = "/user")

        public String user() {

        return user.getName()+user.getAge();

        }

    }

    四、多个环境配置文件

    在现实的开发环境中,我们需要不同的配置环境;格式为application-{profile}.properties,其中{profile}对应你的环境标识,比如

    application-test.properties:测试环境

    application-dev.properties:开发环境

    application-prod.properties:生产环境

    怎么使用?只需要我们在application.yml中加:

    spring:

        profiles:

            active:dev

    其中application-dev.yml:

    server:

    port:8082

    启动工程,发现程序的端口不再是8080,而是8082。

    相关文章

      网友评论

          本文标题:SpringBoot配置文件详解

          本文链接:https://www.haomeiwen.com/subject/ezsjfftx.html