【Spring Boot】7.注入配置文件

1.方法一:spring注解注入

1.1 创建Student.java

package com.example.demo.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;


//注入配置方法一:Spring注解方法
@Component //相当于配置文件中 <bean id="user" class="当前注解的类"/>
public class Student {
    @Value("zhang")
    private String name;
    @Value("21")
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Student() {
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

1.2 在test测试中进行测试

package com.example.demo;

import com.example.demo.pojo.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {
    @Autowired//@Autowired是按类型自动装配的,同一类型的对象需唯一
    private Student student;

    @Test
    void contextLoads() {
        System.out.println(student);
    }
}

在这里插入图片描述

2.方法二:使用yaml注入配置

2.1更改配置文件

把application.properties改成application.yaml
在这里插入图片描述

person:
 name: zhang
 age: 3
 happy: false
 birth: 2000/01/01
 maps: {k1: v1,k2: v2}
 lists:
   - code
   - girl
   - music

2.2 创建Person.java

package com.example.demo.pojo;

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

import java.util.Date;
import java.util.List;
import java.util.Map;

//注入配置方法二:Yaml注入配置文件
@Component// 组件  相当于配置文件中 <bean id="user" class="当前注解的类"/>
@ConfigurationProperties(prefix = "person")
public class Person {
        private String name;
        private Integer age;
        private Boolean happy;
        private Date birth;
        private Map<String,Object> maps;
        private List<Object> lists;

    public Person() {
    }

    public Person(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists) {
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.birth = birth;
        this.maps = maps;
        this.lists = lists;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Boolean getHappy() {
        return happy;
    }

    public void setHappy(Boolean happy) {
        this.happy = happy;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", happy=" + happy +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                '}';
    }
}

2.3 在test测试中进行测试

 @Autowired
    private Person person;

    @Test
    void contextLoads2() {
        System.out.println(person);
    }

在这里插入图片描述

3.使用properties配置文件注入

3.1 创建person2.properties

name=zhang
age=18

3.2创建person2.java

package com.example.demo.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

//注入配置方法三:加载指定配置文件
@Component// 组件  相当于配置文件中 <bean id="user" class="当前注解的类"/>
@PropertySource(value = "classpath:person2.properties")  //绑定Yaml
public class Person2 {
    @Value("${name}")
    private String name;
    @Value("${age}")
    private Integer age;
    private Boolean happy;
    private Date birth;
    private Map<String,Object> maps;
    private List<Object> lists;

    public Person2() {
    }

    public Person2(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists) {
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.birth = birth;
        this.maps = maps;
        this.lists = lists;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Boolean getHappy() {
        return happy;
    }

    public void setHappy(Boolean happy) {
        this.happy = happy;
    }

    public Date getBirth() {
        return birth;
    }

    public void setBirth(Date birth) {
        this.birth = birth;
    }

    public Map<String, Object> getMaps() {
        return maps;
    }

    public void setMaps(Map<String, Object> maps) {
        this.maps = maps;
    }

    public List<Object> getLists() {
        return lists;
    }

    public void setLists(List<Object> lists) {
        this.lists = lists;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", happy=" + happy +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                '}';
    }
}

3.3 测试

在这里插入图片描述

4. @ConfigurationProperties与@Value对比

在这里插入图片描述

  • @ConfigurationProperties只需要写一次即可 , @Value则需要每个字段都添加
  • 松散绑定:yml中写的last-name,这个和lastName是一样的, - 后面跟
    着的字母默认是大写的。这就是松散绑定。
  • JSR303数据校验 , 这个就是我们可以在字段是增加一层过滤器验证 , 可以保证数据的合法性
  • 复杂类型封装,yml中可以封装对象 , 使用value就不支持

4.1 结论

如果我们在某个业务中,只需要获取配置文件中的某个值,可以使用一下 @value;其他情况用yaml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,你可以使用Nacos作为配置中心,以便动态获取配置信息。下面是一些步骤来实现在Spring Boot中读取Nacos配置文件: 1. 添加依赖:在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> ``` 2. 配置Nacos连接信息:在`application.properties`或`application.yml`中添加以下配置: ```yaml spring.cloud.nacos.config.server-addr=${NACOS_SERVER_ADDR:localhost:8848} spring.application.name=your-application-name spring.cloud.nacos.config.namespace=${NACOS_NAMESPACE:} ``` 其中`${NACOS_SERVER_ADDR}`是Nacos服务器地址,`${NACOS_NAMESPACE}`是命名空间,`your-application-name`是你的应用名称。 3. 创建配置类:创建一个`@ConfigurationProperties`注解的类,用于绑定Nacos配置: ```java import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component @ConfigurationProperties(prefix = "your-config-prefix") public class YourConfigProperties { private String property1; private String property2; // getters and setters } ``` 这里的`your-config-prefix`是你在Nacos中存储配置的前缀。 4. 注入配置属性:在需要使用配置的地方,使用`@Autowired`注解将配置类注入: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class YourController { @Autowired private YourConfigProperties configProperties; @GetMapping("/your-endpoint") public String yourEndpoint() { String property1 = configProperties.getProperty1(); String property2 = configProperties.getProperty2(); // 使用配置属性做一些操作 return "Some result"; } } ``` 现在,你可以在`YourController`中使用注入的配置属性来读取Nacos配置文件中的值。 注意:确保Nacos服务器已经启动,并且配置文件已经在Nacos中正确配置。如果需要动态刷新配置,请参考Spring Cloud Alibaba的文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值