002Springboot加载properties文件的3种方式

第1种 使用@Value("${属性key}") 

第2种 使用  @PropertySource(prefix = "属性的前缀")

第3种使用@PropertySources+@PropertySource

第1种 

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
@Data
public class ValueProperties {
//    @Value("other.name") //此方式是指定name的值为other.name
    @Value("${other.name}")
    public String name;
}


 

 


第2种

@Data
public class OtherProperties {

    private String name;

    private String desc;

}

 

配置类:

/**
 * @Description: 加载properties文件
 * 从应用加载的properties的匹配规则
 */
@Configuration
//注释掉有,从应用加载的properties文件中找,主要多个properties注意顺序和覆盖问题
//@PropertySource("application2.properties")  //@1
public class OtherPropertiesConfiguration {

    /**
     * 默认是找程序加载的properties文件,此应用就是application.properties
     */
    @Bean
    @ConfigurationProperties(prefix = "other")
//    @ConfigurationProperties(prefix = "other2") 对应@1
    public OtherProperties otherProperties(){
        return new OtherProperties();
    }
}

application.properties的内容

other.name=other
other.desc=come on

 

第3种

@Data
public class HerPropeties {

    private String name;

    private String desc;

}
@Data
public class MyPropeties {

    private String name;

    private String desc;

}

配置类

@Configuration
@PropertySources({
        @PropertySource("my.properties"),
        @PropertySource("her.properties")
})
public class PropertiesConfiguration {

    @Bean
    @ConfigurationProperties(prefix = "my")
    public MyPropeties myPropetiest(){
        return new MyPropeties();
    }


    @Bean
    @ConfigurationProperties(prefix = "her")
    public HerPropeties herPropetiest(){
        return new HerPropeties();
    }

}

her.properties的内容:

her.name=angle
her.desc=chinese

my.properties的内容

my.name=allen
my.desc=java

测试类

@SpringBootApplication
public class SpringbootPropertiesApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootPropertiesApplication.class, args);
        System.out.println(context.getBean(MyPropeties.class));
        System.out.println(context.getBean(HerPropeties.class));
        System.out.println(context.getBean(OtherProperties.class));
        System.out.println(context.getBean(ValueProperties.class));
    }

}

 

测试结果

MyPropeties(name=allen, desc=java)
HerPropeties(name=angle, desc=chinese)
OtherProperties(name=other, desc=come on)
ValueProperties(name=other)

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot可以通过多方式加载properties文件,其中最常用的方式是使用@PropertySource注解和@ConfigurationProperties注解。 1. 使用@PropertySource注解加载properties文件 @PropertySource注解可以用来指定要加载properties文件的路径。在Spring Boot中,我们通常将properties文件放在src/main/resources目录下。 例如,我们有一个名为config.properties文件,其中包含了一些配置信息,我们可以在Spring Boot的配置类中使用@PropertySource注解来加载这个文件: ``` @Configuration @PropertySource("classpath:config.properties") public class AppConfig { // ... } ``` 2. 使用@ConfigurationProperties注解加载properties文件 @ConfigurationProperties注解可以用来将properties文件中的属性值映射到Java对象的属性中。在Spring Boot中,我们通常将@ConfigurationProperties注解与@Component注解一起使用,将这个Java对象作为一个Bean注册到Spring容器中。 例如,我们有一个名为myconfig.properties文件,其中包含了一些配置信息,我们可以创建一个名为MyConfig的Java类,使用@ConfigurationProperties注解将这些属性值映射到Java对象中: ``` @Component @ConfigurationProperties(prefix = "myconfig") public class MyConfig { private String name; private int age; // getter and setter methods } ``` 在这个例子中,@ConfigurationProperties注解的prefix属性指定了要映射的属性的前缀,即myconfig。这样,当Spring Boot启动时,它会自动将myconfig.properties文件中以myconfig为前缀的属性值映射到MyConfig对象的属性中。 以上就是Spring Boot加载properties文件的两常用方式。 ### 回答2: Spring Boot是一个开发框架,可以帮助开发者快速构建和部署应用程序。当我们使用Spring Boot编写应用程序时,经常需要加载一些配置文件来帮助我们进行配置。其中,properties文件是一常见的配置文件类型,它可以定义应用程序的各参数和属性。 Spring Boot可以通过@ConfigurationProperties注解来加载properties文件。在应用程序中,我们可以定义一个配置类,用来存储properties文件中的各属性和参数。例如: ``` @ConfigurationProperties(prefix = "myapp") public class MyProperties { private String name; private int port; //getter和setter方法 } ``` 这个配置类使用@ConfigurationProperties注解来指定属性的前缀为“myapp”,这意味着我们在properties文件中定义的属性名应该以“myapp”开头。在这个配置类中,我们可以定义各属性,并提供相应的getter和setter方法。 为了让Spring Boot加载这个配置类,我们可以在应用程序入口处使用@EnableConfigurationProperties注解。例如: ``` @SpringBootApplication @EnableConfigurationProperties(MyProperties.class) public class MyApp { public static void main(String[] args) { SpringApplication.run(MyApp.class, args); } } ``` 在这个例子中,我们在@SpringBootApplication注解中添加了@EnableConfigurationProperties(MyProperties.class)注解,使得Spring Boot可以自动加载MyProperties这个配置类。 最后,我们可以在properties文件中定义应用程序的各属性和参数。例如: ``` myapp.name=MyApp myapp.port=8080 ``` 在这个例子中,我们使用了“myapp”作为属性名的前缀,使得Spring Boot可以自动将这些属性值加载到MyProperties这个配置类中的相应属性中。 总之,Spring Boot可以非常方便地加载和使用properties文件。通过@ConfigurationProperties注解和@EnableConfigurationProperties注解,我们可以快速加载和使用任意数量的properties文件。这使得我们可以轻松配置应用程序,并完全掌控应用程序的行为和性能。 ### 回答3: Spring Boot是一基于Java语言的框架,它的核心思想是“约定大于配置”。在Spring Boot中,我们可以使用properties文件来简化配置,这方式比XML配置方便得多。作为Spring Boot的核心功能,如何加载properties文件是一个重要的问题。本文将介绍如何使用Spring Boot加载properties文件,并提供一些实际的例子。 在Spring Boot中,可以使用@PropertySource注解来指定要加载properties文件路径,并使用@Value注解来获取指定属性的值。如果在复杂的应用程序中需要使用多个properties文件,则可以使用@PropertySources注解来指定多个文件路径,或者使用application.properties文件来指定默认值。 对于基于Spring Boot的Web应用程序,可以使用Spring的Environment类来获取properties文件中定义的属性值。通常在类中注入一个Environment类的实例并使用getPropertys方法就可以获取属性值。 在Spring Boot中,配置文件加载顺序是非常重要的。默认情况下,Spring Boot会先加载application.properties文件,然后加载application.yml文件。如果同时存在这两个文件,那么以application.yml的配置为准。 以下是一个加载properties文件的示例。首先创建一个Spring Boot项目,然后创建一个名为application.properties文件,该文件包含配置属性: ``` # application.properties app.name=Spring Boot Properties Example app.description=This is an example of how to use properties in a Spring Boot application. ``` 在代码中使用@Value注解获取配置属性的值: ``` @RestController @RequestMapping("/api") public class ExampleController { @Value("${app.name}") private String appName; @Value("${app.description}") private String appDescription; @GetMapping("/example") public String getExample() { return appName + ": " + appDescription; } } ``` 通过此方式,我们就可以在Spring Boot应用中加载并使用properties文件中的属性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值