Java 运行基于Spring的应用程序时如何解析占位符?
同事们,我有基于java的Spring配置:Java 运行基于Spring的应用程序时如何解析占位符?,java,spring,maven,properties-file,property-placeholder,Java,Spring,Maven,Properties File,Property Placeholder,同事们,我有基于java的Spring配置: @Configuration @EnableTransactionManagement @ComponentScan (basePackages = {"com.abc.dirint"}) @PropertySource("classpath:/settings/${env}/dir.properties") @EnableScheduling public class DirConfig { private static final Lo
@Configuration
@EnableTransactionManagement
@ComponentScan (basePackages = {"com.abc.dirint"})
@PropertySource("classpath:/settings/${env}/dir.properties")
@EnableScheduling
public class DirConfig {
private static final Logger log = LoggerFactory.getLogger(DirConfig.class);
@Autowired
private Environment environment;
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() throws IOException {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setIgnoreUnresolvablePlaceholders(true);
return propertySourcesPlaceholderConfigurer;
}
/*Beans follow...*/
}
当我执行mvncleanpackage-Denv=dev
时,它运行测试并构建项目,没有任何错误
现在我想运行编译过的jar。
我执行java-jar dir-integration-1.2-SNAPSHOT.jar-Denv=dev
,程序失败(这是预期的),下一个堆栈跟踪:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.abc.dirint.DirConfig]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'env' in string value "classpath:/settings/${env}/dir.properties"
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:180)
Wnen我运行$java-jar dir-integration-1.2-SNAPSHOT.jar--env=dev
结果如下:
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.abc.dirint.DirConfig]; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'env' in string value "classpath:/settings/${env}/dir.properties"
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:180)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:308)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:270)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:93)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:686)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:524)
at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
at com.abc.dirint.AdApp.main(AdApp.java:19) Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'env' in string value "classpath:/settings/${env}/dir.properties"
at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236)
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
at org.springframework.core.env.AbstractEnvironment.resolveRequiredPlaceholders(AbstractEnvironment.java:571)
at org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:379)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:
线程“main”org.springframework.beans.factory.BeanDefinitionStoreException中的异常:未能解析配置类[com.abc.dirint.DirConfig];嵌套异常为java.lang.IllegalArgumentException:无法解析字符串值“classpath:/settings/${env}/dir.properties”中的占位符“env”
位于org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:180)
位于org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:308)
位于org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:228)
位于org.springframework.context.support.postprocessorregistrationlegate.invokeBeanDefinitionRegistryPostProcessors(postprocessorregistrationlegate.java:270)
位于org.springframework.context.support.postprocessorregistrationlegate.invokeBeanFactoryPostProcessors(postprocessorregistrationlegate.java:93)
位于org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:686)
位于org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:524)
位于org.springframework.context.annotation.AnnotationConfigApplicationContext。(AnnotationConfigApplicationContext.java:84)
在com.abc.dirint.AdApp.main(AdApp.java:19)上,由于以下原因导致:java.lang.IllegalArgumentException:无法解析字符串值“classpath:/settings/${env}/dir.properties”中的占位符“env”
位于org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
位于org.springframework.util.PropertyPlaceholderHelper.ReplacePlaceHolderHelper(PropertyPlaceholderHelper.java:126)
位于org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:236)
位于org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210)
位于org.springframework.core.env.AbstractEnvironment.resolveRequiredPlaceholders(AbstractEnvironment.java:571)
位于org.springframework.context.annotation.ConfigurationClassParser.processPropertySource(ConfigurationClassParser.java:379)
位于org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:
在应用程序运行期间,如何从指定的属性文件接收属性?从
java-h
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)
where options include:
...
-D<name>=<value>
set a system property
用法:java[-options]类[args…]
(执行一个类)
或java[-options]-jar jarfile[args…]
(执行jar文件)
其中包括:
...
-D=
设置系统属性
因此,使用提供的系统属性运行jar的正确命令应该是java-Denv=dev-jar dir-integration-1.2-SNAPSHOT.jar
而且,您应该知道,使用
mvn…-Denv=dev
vsjava-Denv=dev…
是两件完全不同的事情
使用mvn
,占位符替换在编译时发生,这意味着最终的jar将包含@PropertySource(“classpath:/settings/dev/dir.properties”)
但是,您的第二种方法是将占位符保留在已编译类中,并依靠Spring在运行时执行替换。这可能会帮助您:尝试运行为:java-Denv=dev-jar dir-integration-1.2-SNAPSHOT.jar