SpringBoot 3:配置文件

1、properties

同以前的properties用法

2、yaml

简介

YAML 是 “YAML Ain’t Markup Language”(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:“Yet Another Markup Language”(仍是一种标记语言)。
非常适合用来做以数据为中心的配置文件

基本语法

• key: value;kv之间有空格
• 大小写敏感
• 使用缩进表示层级关系
• 缩进不允许使用tab,只允许空格
• 缩进的空格数不重要,只要相同层级的元素左对齐即可
• '#‘表示注释
• 字符串无需加引号,如果要加,’'与""表示字符串内容 会被 转义/不转义

数据类型

• 字面量:单个的、不可再分的值。date、boolean、string、number、null

k: v

• 对象:键值对的集合。map、hash、set、object

行内写法:  k: {k1:v1,k2:v2,k3:v3}
#或
k: 
  k1: v1
  k2: v2
  k3: v3

• 数组:一组按次序排列的值。array、list、queue

行内写法:  k: [v1,v2,v3]
#或者
k:
 - v1
 - v2
 - v3

举个栗子

1、新建实体类 person和pet

person类

@Data
/**
 * 为了生效,使用 @Component,将car组件加入到容器中
 * 只有在容器中的组件,才可拥有spring容器(springboot)提供的强大的注解功能,比如@ConfigurationProperties
 */
@ConfigurationProperties(prefix = "person")
@Component 
public class Person {
    
    private String userName;
    private Boolean boss;
    private Date birth;
    private Integer age;
    private Pet pet;
    private String[] interests;
    private List<String> animal;
    private Map<String, Object> score;
    private Set<Double> salarys;
    private Map<String, List<Pet>> allPets;
}

宠物类

@Data
public class Pet {
    private String name;
    private Double weight;
}

2、application.yaml

person:
  userName: zhangsan
  boss: false
  birth: 2019/12/12 20:12:33
  age: 18
  pet:
    name: tomcat
    weight: 23.4
  interests: [篮球,游泳]
  animal:
    - jerry
    - mario
  score:
    english:
      first: 30
      second: 40
      third: 50
    math: [131,140,148]
    chinese: {first: 128,second: 136}
  salarys: [3999,4999.98,5999.99]
  allPets:
    sick:
      - {name: jack,weight: 20}
      - {name: tom}
      - {name: jerry,weight: 47}
    health: [{name: mario,weight: 47}]

3、测试

@Controller
public class HelloSpringBoot {

    @Autowired
    Person person;

	@ResponseBody
    @RequestMapping("/person")
    public Person test3(){
        return person;
    }
}

2、配置提示
自定义的类和配置文件绑定一般没有提示。spring类配置属性的时候就有提示,我们如下操作解决

导入pom依赖,重启即可自动提示了

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-configuration-processor</artifactId>
   <optional>true</optional>
</dependency>
 	<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!--打包的时候,排除配置处理器,因为只是yaml提示功能,没必要打包进去-->
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!针对您的问题,创建前缀一样的文件可以使用Spring框架自带的`@ConfigurationProperties`注解,将同一前缀的属性封装到同一个对象中。具体实现步骤如下: 1. 在`application.properties`配置文件中添加属性: ``` myapp.datasource.url=jdbc:mysql://localhost:3306/mydb myapp.datasource.username=root myapp.datasource.password=123456 ``` 2. 创建一个`MyAppProperties`类,使用`@ConfigurationProperties`注解标记,并指定前缀: ``` @Configuration @ConfigurationProperties(prefix = "myapp.datasource") public class MyAppProperties { private String url; private String username; private String password; // 省略getter和setter方法 } ``` 3. 在Spring Boot的主类中添加`@EnableConfigurationProperties`注解,并将`MyAppProperties`类作为参数传入: ``` @SpringBootApplication @EnableConfigurationProperties(MyAppProperties.class) public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } ``` 4. 在需要使用属性的地方注入`MyAppProperties`对象即可: ``` @Service public class MyService { private final MyAppProperties properties; public MyService(MyAppProperties properties) { this.properties = properties; } public void doSomething() { String url = properties.getUrl(); String username = properties.getUsername(); String password = properties.getPassword(); // 使用属性进行业务逻辑处理 } } ``` 这样就可以方便地管理同一前缀的属性了。希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值