SpringBoot读取配置@ConfigurationProperties @ImportResource

本文介绍了如何在SpringBoot中使用@ConfigurationProperties来绑定配置文件的复杂结构,以及利用@Value注解获取配置文件中的值。同时,提到了PropertySource用于加载额外的属性源,以及@ImportResource导入XML配置文件的功能。
摘要由CSDN通过智能技术生成

Configuration Metadata

SpringBootIntegration: SpringBootIntegration

依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <scope>provided</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
    </dependencies>

@ConfigurationProperties

配置文件yml还是properties他们都能获取到值;

我们只是在某个业务逻辑中需要获取一下配置文件中的某项值,使用@Value

我们专门编写了一个javaBean来和配置文件进行映射,我们就直接使用@ConfigurationProperties

product:

  courseDTOS:

    - id: 111

      num: 4444

    - id: 222

      num: 555

    - id: 333

      num: 666

@Data

@Configuration

@ConfigurationProperties("product")

public class ConfigurationPropertiesShow {

    private List<CourseDTO> courseDTOS;

}

Value 

类型

基本数值

可以写SpEL; #{}

可以写${};取出配置文件中的值

${person.nickName:默认值}  没有默认值会报错

public class Person {
   @Value("zs")
   private String name;
   @Value("#{20-2}")
   private Integer age;
   
   @Value("${person.nickName:默认值}")
   private String nickName;
    person.nickName=kk
 
public class ValuesShowTest {
 
    @Test
    public void test(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ValuesShow.class);
 
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }
        Person person = (Person)applicationContext.getBean("person");
        System.out.println(person.toString());
       
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        System.out.println(environment.getProperty("person.nickName"));
    }
}
person:
  name: zhangsan
  age: 18
@Controller
public class QuickStartController {
    @Value("${person.name:du}")
    private String name;
    @Value("${person.age}")
    private Integer age;
​
    @RequestMapping("/quick")
    @ResponseBody
    public String quick(){
        return "springboot 访问成功! name="+name+",age="+age;
    }
}

PropertySource

system.ip=http://192.168.31.206:8888/metadataserver/v1
system.login=${system.ip}/pro/proUserController/getToken
# map 结构
system.role[select]=${system.ip}/system/user/select
system.role[update]=${system.ip}/system/user/update
@Component
@PropertySource(value = {"classpath:otherSystemInterfaceConfig/usereSystem.properties"})
@ConfigurationProperties(prefix = "system")
@Getter
@Setter
public class PropertiesShow {
    private String login;
    private Map<String, String> role;
}

@ImportResource 

导入以前的xml配置文件,让其生效

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
​
<!--  构造函数注入  -->
    <bean id="now" class="java.util.Date"/>
    <bean id="account" class="com.du.bean.Account">
        <constructor-arg name="name" value="泰斯特"/>
        <constructor-arg name="age" value="18"/>
        <constructor-arg name="birthday" ref="now"/>
    </bean>
</beans>
@ImportResource(locations = {"classpath:DIBeanTest.xml"})
@Component
public class ImportResourceShow {
}

@SpringBootTest
@ExtendWith(SpringExtension.class)
public class ImportResourceTest {
​
    @Autowired
    ApplicationContext applicationContext;
​
    @Test
    public void test(){
        boolean account = applicationContext.containsBean("account");
        System.out.println(account);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值