@ConfigurationProperties代替@Value

1、配置文件预览

application-local.yml

spring:
  profiles: local
2、配置类
@Data
@Component
@ConfigurationProperties(prefix = "spring")
public class SpringProperties {
    private String profiles;

}

@Data是为了get到值。
@Component是注入到上下文中
@ConfigurationProperties定义当前类是配置类。prefix和配置文件中的前缀一致。

3、使用
1、先引入到当前类
@Autowired
private SpringProperties springProperties;

2、在方法中使用
if (SystemConst.ENV_LOCAL.equals(springProperties.getProfiles())){
            
}

你可能感兴趣的:(JavaWeb)