首页后端开发JAVASpringboot读取配置的10种方式(springboot读取配置文件源码)

Springboot读取配置的10种方式(springboot读取配置文件源码)

时间2023-03-27 14:22:25发布访客分类JAVA浏览501
导读:Spring Boot 读取配置文件的方式有很多,下面介绍 10 种常用方式以及相应的示例和解析。@Value 注解 使用 @Value 注解可以直接将配置文件中的值注入到对应的属性上,示例如下:@Value("${spring.datas...

Spring Boot 读取配置文件的方式有很多,下面介绍 10 种常用方式以及相应的示例和解析。

  1. @Value 注解 使用 @Value 注解可以直接将配置文件中的值注入到对应的属性上,示例如下:
@Value("${
spring.datasource.url}
    ")
private String url;

这里的 ${ spring.datasource.url} 是从 application.yml 或 application.properties 文件中读取的,使用此注解需要在类上添加 @Component 注解。

  1. @ConfigurationProperties 注解 使用 @ConfigurationProperties 注解可以将配置文件中的多个属性一次性注入到一个类中,示例如下:
@ConfigurationProperties(prefix = "spring.datasource")
public class DataSourceProperties {
    
    private String url;
    
    private String username;
    
    private String password;

    // ...
}
    

这里的 prefix 为配置文件中的前缀,需要在类上添加 @Component 注解。

  1. Environment 对象 使用 Environment 对象可以获取配置文件中的属性值,示例如下:
@Autowired
private Environment env;

public void test() {
    
    String url = env.getProperty("spring.datasource.url");

}
  1. @PropertySource 注解 使用 @PropertySource 注解可以指定读取的配置文件,示例如下:
@Configuration
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfig {

    @Value("${
jdbc.url}
    ")
    private String url;

    // ...
}

```

这里的 classpath:jdbc.properties 是指定了要读取的配置文件,需要在类上添加 @Configuration 注解。

  1. @ImportResource 注解 使用 @ImportResource 注解可以导入 XML 配置文件,示例如下:
@Configuration
@ImportResource(locations = "classpath:applicationContext.xml")
public class XmlConfig {

}

这里的 classpath:applicationContext.xml 是指定了要导入的 XML 配置文件,需要在类上添加 @Configuration 注解。

  1. Spring Expression Language(SpEL) 使用 SpEL 可以在代码中直接使用配置文件中的属性值,示例如下:
@Value("#{
${
spring.datasource}
.url}
    ")
private String url;

这里的 ${ spring.datasource} .url 是指定了要读取的属性值,需要在类上添加 @Component 注解。

  1. PropertySourcesPlaceholderConfigurer 使用 PropertySourcesPlaceholderConfigurer 可以在 XML 配置文件中使用 SpEL,示例如下:
Copy code
bean class="org.springframework.beans.factory.config.PropertySourcesPlaceholderConfigurer">
    
    property name="locations">
    
        list>
    
            value>
    classpath:jdbc.properties/value>
    
        /list>
    
    /property>
    
/bean>
    
bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

    property name="driverClassName" value="${
jdbc.driverClassName}
    " />

    property name="url" value="${
jdbc.url}
    " />

    property name="username" value="${
jdbc.username}
    " />

    property name="password" value="${
jdbc.password}
    " />
    
/bean>
```

这里的 classpath:jdbc.properties 是指定了要读取的配置文件。

8. ApplicationArguments 对象
使用 ApplicationArguments 对象可以获取命令行参数,示例如下:

```java

Copy code

@Component

public class MyBean implements CommandLineRunner {

@Override
public void run(ApplicationArguments args) throws Exception {
    
    String[] argArray = args.getSourceArgs();
}

} `

  1. 自定义配置文件 使用自定义配置文件可以按照自己的需求读取配置文件,示例如下:
Copy code
@Configuration
@PropertySource("classpath:my-config.properties")
public class MyConfig {

    @Value("${
my.config.value}
    ")
    private String myConfigValue;

    // ...
}
```

这里的 classpath:my-config.properties 是指定了要读取的配置文件,需要在类上添加 @Configuration 注解。

10. 自定义属性编辑器
使用自定义属性编辑器可以将配置文件中的属性值转换成对应的类型,示例如下:

```java

Copy code

public class MyPropertyEditor extends PropertyEditorSupport {

@Override
public void setAsText(String text) throws IllegalArgumentException {
    
    setValue(text == null ? null : new MyConfig(text));
}

} `

这里的 MyConfig 是自定义的类型,需要在使用处指定使用此编辑器:

@InitBinder

public void initBinder(WebDataBinder binder) {

binder.registerCustomEditor(MyConfig.class, new MyPropertyEditor());

} `

以上就是 Spring Boot 读取配置文件的 10 种方式以及相应的示例和解析,根据实际需要选择合适的方式即可。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

java

若转载请注明出处: Springboot读取配置的10种方式(springboot读取配置文件源码)
本文地址: https://pptw.com/jishu/369.html
漫谈模式之行为模式小结(行为模式的概念) 学习研究Jrebel自建激活服务器 – 支持全部版本IDEA

游客 回复需填写必要信息