2017-03-16 26 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

相關問題