2017-03-16 3 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: 

私はからプロパティを受け取るために何をすべきアプリケーションの実行中に指定されたプロパティファイル? 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 

から

+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

そこで提供されるシステムプロパティを使用してjarファイルを実行するための正しいコマンドはjava -Denv=dev -jar dir-integration-1.2-SNAPSHOT.jar代わり


そして、あなたはjava -Denv=dev ...mvn ... -Denv=devを使用して2である、ことを認識する必要がありますする必要があります全く違うもの。 mvn

、プレースホルダの交換は、最終的な瓶の意志が@PropertySource("classpath:/settings/dev/dir.properties")

が含まれていることを意味する、時間をコンパイルで起こるしかし、あなたの第二のアプローチは、置き換えを実行するためにコンパイルされたクラスでプレースホルダを維持し、春に依存することですランタイムで

+0

Adリアン・シャム、ありがとう。 – May12

関連する問題