8

作業中のプロジェクトを、SpringBoot コマンド ライン引数の使用からファイルからのプロパティの読み取りに移行しています。@Configurationクラスの関連部分は次のとおりです。

@Configuration
class RemoteCommunication {

    @Inject
    StandardServletEnvironment env


    @Bean
    static PropertySourcesPlaceholderConfigurer placeholderConfigurer () {
        // VERIFIED this is executing...
        PropertySourcesPlaceholderConfigurer target = new PropertySourcesPlaceholderConfigurer()
        // VERIFIED this files exists, is readable, is a valid properties file
        target.setLocation (new FileSystemResource ('/Users/me/Desktop/mess.properties'))
        // A Debugger does NOT show this property source in the inject Environment
        target
    }


    @Bean  // There are many of these for different services, only one shown here.
    MedicalSorIdService medicalSorIdService () {
        serviceInstantiator (MedicalSorIdService_EpicSoap, 'uri.sor.id.lookup.internal')
    }


    // HELPER METHODS...


    private <T> T serviceInstantiator (final Class<T> classToInstantiate, final String propertyKeyPrimary) {
        def value = retrieveSpringPropertyFromConfigurationParameter (propertyKeyPrimary)
        classToInstantiate.newInstance (value)
    }


    private def retrieveSpringPropertyFromConfigurationParameter (String propertyKeyPrimary) {
        // PROBLEM: the property is not found in the Environment
        def value = env.getProperty (propertyKeyPrimary, '')
        if (value.isEmpty ()) throw new IllegalStateException ('Missing configuration parameter: ' + "\"$propertyKeyPrimary\"")
        value
    }

を使用@Valueしてプロパティを注入することEnvironmentできますが、可能であれば直接操作したいと思います。設定がにない場合、どこからそれらを引っ張っているのEnvironmentか正確にはわかりません...@Value

env.getProperty()ただし、プロパティを指定するコマンドライン引数を渡すと、引き続きうまく機能します。

どんな提案も大歓迎です!

4

4 に答える 4

5

SpringBoot では、@EnableConfigurationPropertiesアノテーションを使用するだけで十分です。セットアップする必要はありませんPropertySourcesPlaceholderConfigurer

次に、POJO でアノテーション @ConfigurationProperties を追加すると、Spring は application.properties で定義されたプロパティを自動的に挿入します。

YAML ファイルを使用することもできます。適切な依存関係 (SnakeYaml など) をクラスパスに追加するだけです。

ここで詳細な例を見つけることができます: http://spring.io/blog/2013/10/30/empowering-your-apps-with-spring-boot-s-property-support

于 2014-01-13T22:14:53.937 に答える
1

たぶん、必要なのは-Dspring.config.location=...(またはSPRING_CONFIG_LOCATIONenv varとして)設定することだけですか?これには、実行時にアプリのデフォルト パスに追加の構成ファイルを追加する効果があり、通常のapplication.properties? 詳細については、ハウツー ドキュメントを参照してください。

于 2014-01-14T13:26:12.707 に答える