Bob*_*obo 128 java spring spring-profiles
我需要根据不同的当前环境配置文件编写不同的逻辑.如何从Spring获取当前的活动和默认配置文件?
awe*_*old 188
你可以自动装配 Environment
@Autowired
Environment env;
Run Code Online (Sandbox Code Playgroud)
Environment 提供:
String[] getActiveProfiles(),String[] getDefaultProfiles(),和 boolean acceptsProfiles(String... profiles)Mar*_*ima 53
扩展User1648825的简单答案(我无法评论,我的编辑被拒绝):
@Value("${spring.profiles.active}")
private String activeProfile;
Run Code Online (Sandbox Code Playgroud)
如果没有设置配置文件,这可能会抛出IllegalArgumentException(我得到一个空值).如果您需要设置它,这可能是一件好事; 如果不使用@Value的'default'语法,即:
@Value("${spring.profiles.active:Unknown}")
private String activeProfile;
Run Code Online (Sandbox Code Playgroud)
...如果无法解析spring.profiles.active,则activeProfile现在包含"Unknown"
ana*_*ocs 40
这是一个更完整的例子.
Autowire环境
首先,您需要自动装配环境bean.
@Autowired
private Environment environment;
Run Code Online (Sandbox Code Playgroud)
检查活动配置文件中是否存在配置文件
然后,您可以使用getActiveProfiles()以查明配置文件是否存在于活动配置文件列表中.下面是一个示例,它接受String[]from getActiveProfiles(),从该数组中获取流,然后使用匹配器检查多个配置文件(Case-Insensitive),如果它们存在则返回一个布尔值.
//Check if Active profiles contains "local" or "test"
if(Arrays.stream(environment.getActiveProfiles()).anyMatch(
env -> (env.equalsIgnoreCase("test")
|| env.equalsIgnoreCase("local")) ))
{
doSomethingForLocalOrTest();
}
//Check if Active profiles contains "prod"
else if(Arrays.stream(environment.getActiveProfiles()).anyMatch(
env -> (env.equalsIgnoreCase("prod")) ))
{
doSomethingForProd();
}
Run Code Online (Sandbox Code Playgroud)
您还可以使用注释实现类似的功能@Profile("local")以下是有关此技术的更多信息:Spring Profiles
use*_*825 23
@Value("${spring.profiles.active}")
private String activeProfile;
Run Code Online (Sandbox Code Playgroud)
它可以工作,您不需要实现EnvironmentAware.但我不知道这种方法的缺点.
naz*_*art 22
正如前面已经提到的。您可以自动装配环境:
@Autowired
private Environment environment;
Run Code Online (Sandbox Code Playgroud)
只有你可以更轻松地检查所需的环境:
if (environment.acceptsProfiles(Profiles.of("test"))) {
doStuffForTestEnv();
} else {
doStuffForOtherProfiles();
}
Run Code Online (Sandbox Code Playgroud)
Mic*_*ael 11
似乎有一些需求能够静态访问它。
\n\n\n\n\n如何在非 spring 管理的类中的静态方法中获得这样的东西?\xe2\x80\x93 以太币
\n
这是一个 hack,但您可以编写自己的类来公开它。您必须小心确保SpringContext.getEnvironment()在创建所有 bean 之前不会调用任何内容,因为无法保证该组件何时会被实例化。
@Component\npublic class SpringContext\n{\n private static Environment environment;\n\n public SpringContext(Environment environment) {\n SpringContext.environment = environment;\n }\n\n public static Environment getEnvironment() {\n if (environment == null) {\n throw new RuntimeException("Environment has not been set yet");\n }\n return environment;\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
小智 6
如果您既不想使用 @Autowire 也不想注入 @Value 您可以简单地执行以下操作(包括后备):
System.getProperty("spring.profiles.active", "unknown");
Run Code Online (Sandbox Code Playgroud)
这将返回任何活动的配置文件(或回退到“未知”)。
要稍微调整以处理未设置变量的情况,您可以使用默认值:
@Value("${spring.profiles.active:unknown}")
private String activeProfile;
Run Code Online (Sandbox Code Playgroud)
这样,如果spring.profiles.active设置了,它将采用它,否则它将采用默认值unknown。
所以不会触发任何异常。并且无需@ActiveProfiles("test")在测试中强制添加类似的内容以使其通过。
| 归档时间: |
|
| 查看次数: |
106994 次 |
| 最近记录: |