Spring Boot读取配置的几种方式

Spring Boot读取配置的几种方式

读取application文件
在application.yml或者properties文件中添加:
info.address=USA
info.company=Spring
info.degree=high

  • @Value注解读取方式
package ziania.dailylife;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component
public class ConfigTest {

    @Value("${info.address}")
    private String address;

    @Value("${info.company}")
    private String company;

    @Value("${info.degree}")
    private String degree;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getDegree() {
        return degree;
    }

    public void setDegree(String degree) {
        this.degree = degree;
    }
}
  • @ConfigurationProperties注解读取方式
package ziania.dailylife;

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

@Component
@ConfigurationProperties(prefix="info")
public class ConfigTest {

    private String address;

    private String company;

    private String degree;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getDegree() {
        return degree;
    }

    public void setDegree(String degree) {
        this.degree = degree;
    }
}

读取指定文件
资源目录下建立config/db-config.properties:
db.username=root
db.password=123456

  • @PropertySource+@Value注解读取方式(@PropertySource不支持yml文件读取)
package ziania.dailylife;

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

@Component
@PropertySource(value="conf.properties")
public class ConfigTest {

    @Value("${info.address}")
    private String address;

    @Value("${info.company}")
    private String company;

    @Value("${info.degree}")
    private String degree;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getDegree() {
        return degree;
    }

    public void setDegree(String degree) {
        this.degree = degree;
    }
}
  • @PropertySource+@ConfigurationProperties注解读取方式
package ziania.dailylife;

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

@Component
@ConfigurationProperties(prefix="info")
@PropertySource(value="conf.properties")
public class ConfigTest {

    private String address;

    private String company;
    
    private String degree;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getDegree() {
        return degree;
    }

    public void setDegree(String degree) {
        this.degree = degree;
    }
}
  • Environment读取方式
@Autowired
private Environment env;

// 获取参数
String getProperty(String key);
### 回答1: C知道回答:开启 Spring Boot 特性的方式有以下几种: 1. 使用 @EnableAutoConfiguration 注解 2. 使用 @SpringBootApplication 注解 3. 使用 @Configuration 和 @Import 注解手动导入配置类 4. 使用 XML 配置文件手动配置 Spring Boot 特性 ### 回答2: 开启 Spring Boot 特性有以下几种方式: 1. 使用@EnableAutoConfiguration注解:通过在主配置类上添加@EnableAutoConfiguration注解,可以自动开启Spring Boot的自动配置特性。该注解会根据项目中的依赖关系自动配置相应的组件和配置项,简化项目配置工作。 2. 使用@SpringBootApplication注解:@SpringBootApplication是Spring Boot项目的入口注解,它包含了@EnableAutoConfiguration和@Configuration注解。使用@SpringBootApplication注解可以一次性开启Spring Boot的自动配置、组件扫描和配置类的扫描等特性。 3. 使用starter依赖:Spring Boot提供了一系列的starter依赖,每个starter都定义了一组常用的依赖项。通过在项目中引入相应的starter依赖,可以方便地开启特定的功能特性。比如,使用spring-boot-starter-web依赖可以开启Web开发相关的特性。 4. 自定义配置类:在Spring Boot中,我们可以通过编写自定义的配置类来开启一些特定的功能。通过在配置类上使用@Configuration注解,我们可以定义一些@Bean的方法来配置特定的组件或特性。通过将这个配置类注册为Bean,在应用启动时自动生成相应的组件。 综上所述,使用@EnableAutoConfiguration注解、@SpringBootApplication注解、starter依赖和自定义配置类等方式都可以帮助开启Spring Boot的特性。根据项目需求和实际情况,选择适合的方式来快速搭建和配置Spring Boot应用。 ### 回答3: 开启 Spring Boot 特性有以下几种方式: 1. 使用 @EnableAutoConfiguration 注解:Spring Boot 基于约定优于配置的原则,自动配置了很多常用的特性。通过在主配置类上加上 @EnableAutoConfiguration 注解,可以启用自动配置特性。该注解会根据项目使用的依赖自动配置相关的 Bean,简化了配置过程。 2. 配置文件属性:Spring Boot 通过读取配置文件中的属性来设置特性。可以通过在 application.properties 或 application.yml 配置文件中设置相关属性来开启特性。例如,可以设置 spring.data.mongodb.uri 属性来启用 MongoDB 的支持。 3. 使用 @Conditional 注解:Spring Boot 提供了很多条件注解,利用这些注解可以根据条件来选择性地开启特定的特性。例如,可以使用 @ConditionalOnClass 注解来判断某个类是否在 classpath 中存在,从而决定是否启用某个特性。 4. 自定义配置类:Spring Boot 提供了一个 @Configuration 注解,通过在该注解下定义相关的配置类,可以进行更细粒度的配置。在配置类中可以使用 @Bean 注解配置特性相关的 Bean,从而启用特性。 5. 外部化配置Spring Boot 可以通过外部化配置来灵活地开启和配置特性。可以通过命令行参数、环境变量、属性文件等方式来设置特性相关的配置项。 总之,Spring Boot 提供了多种开启特性的方式,开发人员可以根据项目需求和个人偏好选择适合的方式来启用特性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值