在Spring 和 Spring Boot中使用配置属性

在Spring 和 Spring Boot中使用配置属性

      • 一、在spring中使用配置属性
        • 1.1 手动指定配置文件
        • 1.2 获取属性值
      • 二、在Spring Boot 中使用配置属性
        • 2.1 指定配置文件
        • 2.2 获取属性值
      • 总结

一、在spring中使用配置属性

1.1 手动指定配置文件

在spring 中使用@PropertySource注释选择属性文件,可以选择多个。

@Configuration
@PropertySource("classpath:foo.properties")
@PropertySource("classpath:bar.properties")
public class PropertiesWithJavaConfig {
    //...
}

// 动态选择
@PropertySource({ 
  "classpath:persistence-${envTarget:mysql}.properties"
})
...
1.2 获取属性值
// 使用@Value注入
@Value( "${jdbc.url}" )
private String jdbcUrl;

// 使用@Value注入,带默认值
@Value( "${jdbc.url:aDefaultUrl}" ) 
private String jdbcUrl;


// 使用Environment API获取
@Autowired
private Environment env;
...
dataSource.setUrl(env.getProperty("jdbc.url"));

二、在Spring Boot 中使用配置属性

2.1 指定配置文件
  • 默认文件

无特殊需要,不用指定配置文件,从默认位置加载。

src/main/resource目录下的application.properties,测试时使用src/test/resource目录。

  • 运行时在命令行中指定(一般用于生产环境,使用外部配置)
java -jar app.jar --spring.config.location=classpath:/another-location.properties

-- 使用通配符指定多个文件
java -jar app.jar --spring.config.location=config/*/

-- 使用命令行直接指定某个属性的值
java -jar app.jar --property="value"
  • 不同环境默认选择不同的配置文件

可以定义不同环境的配置文件,spring boot 会自动查找,例如application-dev.properties, application-test.properties。

  • 测试时,可以手动选择文件或指定属性值

测试的属性文件默认会在src/test/resource目录中查找,也可以自己选择使用@TextPropertySource。

@RunWith(SpringRunner.class)

// 三种方式选择一种即可
// 方式一:使用@TestPropertySource选择文件
@TestPropertySource("/foo.properties")
// 方式二:不使用文件,直接指定某个名称和值
@TestPropertySource(properties = {"foo=bar"})
// 方式三:使用@SpringBootTest 的properties参数
@SpringBootTest( properties = {"foo=bar"}, classes = SpringBootPropertiesTestApplication.class)
public class FilePropertyInjectionUnitTest {

    @Value("${foo}")
    private String foo;

    @Test
    public void whenFilePropertyProvided_thenProperlyInjected() {
        assertThat(foo).isEqualTo("bar");
    }
}
2.2 获取属性值

与spring类似,使用@Value注解、Environment API来获取属性的值,另外还可以通过@ConfigurationProperties注解将属性绑定到java对象中使用。

例如对于数据库属性,可以映射为java对象,在代码中使用。

database.url=jdbc:postgresql:/localhost:5432/instance
database.username=foo
database.password=bar

使用@ConfigurationProperties映射成java对象(需要在启动类上添加@ConfigurationPropertiesScan,会自动扫描并注册@ConfigurationProperties)

@ConfigurationProperties(prefix = "database")
public class Database {
    String url;
    String username;
@NotBlank
  // 属性上可以添加验证,这样配置的属性不符合要求时,会保IllegalStateException无法启动

  @NotBlank

  @Length(max = 4, min = 1)


    String password;

    // standard getters and setters
}


// 可以在@Bean方法上使用@ConfigurationProperties,将属性绑定到Bean对象上(适用于第三方组件的对象)
@Configuration
public class ConfigProperties {

    @Bean
    @ConfigurationProperties(prefix = "item")
    public Item item() {
        return new Item();
    }
}



// 上面是通过属性的setter方法注入的,也可以通过配置类的构造函数注入,此时属性都是final不可变的。
// 使用@ConstructorBinding绑定配饰熟悉感
@ConfigurationProperties(prefix = "mail.credentials")
public class ImmutableCredentials {

    private final String authMethod;
    private final String username;
    private final String password;

  // spring boot2.6以后,如果只有一个构造函数,可以省略注解


@ConstructorBinding
    public ImmutableCredentials(String authMethod, String username, String password) {
        this.authMethod = authMethod;
        this.username = username;
        this.password = password;
    }

    public String getAuthMethod() {
        return authMethod;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }
}

另外,spring boot支持使用YAML文件,后缀是.yaml、.yml,分层更直观

database:
  url: jdbc:postgresql:/localhost:5432/instance
  username: foo
  password: bar
secret: foo

YMAL的优先级:外部文件会覆盖jar中的文件。

总结

Spring中使用配置属性,需要先通过@PropertySource指定属性文件,然后通过@Value或者Environment API获取属性值。
Spring Boot中使用配置属性,如果在默认位置,可以不用指定属性文件,然后也可以通过@Value或者Environment API获取属性值,另外支持通过@ConfigurationProperties将属性映射为java对象,方便使用。Spring Boot还提供了其他功能方便配置文件的使用。

你可能感兴趣的:(spring,spring,boot,java)