2016-03-30 11 views
3

としてYMLセクションをロードするために、私が設定している以下:私はjava.util.Propertiesとして上記を読みたいどのように私の春のブートアプリケーションでjava.util.Properties

server: 
    host: a.com 
    port: 5922 
    enable-log: true 

を。私は、次のクラスを入れてみました:

@ConfigurationProperties(prefix = "server") 
public class ServerConfig { 
    private Properties serverProps; 
    // ... Getter/setter 
} 

ブート設定ファイルは次のようになります。

@Configuration 
@ComponentScan("com.demo") 
@EnableAspectJAutoProxy(proxyTargetClass = true) 
@EnableConfigurationProperties({ServerConfig.class}) 
@Profile({"dev"}) 
public class TestAppConfiguration { 
} 

@EnableAutoConfiguration 
@SpringBootApplication 
public class TestAppInitializer { 
    public static void main(final String[] args) { 
    SpringApplication.run(TestAppInitializer.class, args); 
    } 
} 

ユニットテストクラス:

@SpringApplicationConfiguration(classes = {TestAppInitializer.class}) 
@ActiveProfiles("dev") 
public class ServerConfigTest extends AbstractTestNGSpringContextTests { 
    @Autowired 
    private ServerConfig serverConfig; 

    @Test 
    public void printDetails() { 
    logger.debug("ServerConfig.properties --> {}", serverConfig.getProperties()); 
    } 
} 

出力は ServerConfig.properties --> nullです。

なぜこのようなことが起こっているのかわかりません。唯一の考えは、私の考えでは、serverの下の設定パラメータは基本的にフラットな文字列です。 Propertiesと読むことができますか?

+1

。あなたはString/Integer/Booleanなどの単純な型のクラス変数にそれらを読むことができます。 –

+0

こんにちは@Niranjan、あなたは直接Propertiesオブジェクトの値を設定する方法を見つけましたか?フィールドを作成してプロパティオブジェクトに追加することができます – Tatha

答えて

1

AFAIKの場合、java.util.Propertiesに注釈@ConfigurationPropertiesを読み込むことはできません。

あなたは、これを作成する必要があります:あなたはConfigurationPropertiesアノテーションを使用してjava.util.Propertiesクラスにproperiesを読み取ることができません

@Component 
@ConfigurationProperties(prefix = "server") 
public class ServerConfig { 
    private String host; 
    private String port; 
    private String enableLog; 
    // ... Getter/setter 
} 
+0

私はすでにこれを行っていました。誰かが 'Properties'をやったと思ったら質問を投稿しました。とにかく、共にありがとう。 – Niranjan

関連する問題