2017-03-16 59 views
2

的同事,我有基于Java Spring配置:如何在运行基于Spring的应用程序时解析占位符?

@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...*/ 

     } 

当我执行mvn clean package -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: 

我应该怎么做才能收到属性应用程序运行时指定的属性文件

+0

这可能会帮助您:http://stackoverflow.com/questions/33855713/spring-boot-running-a-fully-executable-jar-and-specify-d-properties – freakman

+1

尝试运行为: java -Denv = dev -jar dir-integration-1.2-SNAPSHOT.jar – htulsiani

回答

3

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 -Denv=dev -jar dir-integration-1.2-SNAPSHOT.jar代替


系统属性运行JAR和,你应该知道,使用mvn ... -Denv=dev VS java -Denv=dev ...是二完全不同的东西。

随着mvn,占位符替换发生在编译时,这意味着最终的罐子将包含@PropertySource("classpath:/settings/dev/dir.properties")

然而,你的第二个方法是保持在编译的类占位符,并依赖于Spring进行更换在运行

+0

Ad里安沉,谢谢。 – May12

相关问题