0

私はBOOT-INF/libの中に依存するJARを持つSpringブートJAR MyMain.jarを持っています。Springboot依存するjarのクラスパス内のリソースにアクセスできません

BOOT-INF/lib/MyDep.jar/abcd.properties内のプロパティファイルにアクセスしようとしています。

私は以下のコードを試しました。

InputStream in = new ClassPathResource("abcd.properties").getInputStream(); 
System.out.println("InputStream : "+in); 
String line; 
BufferedReader br = new BufferedReader(new InputStreamReader(in));   
while ((line = br.readLine()) != null) { 
    System.out.println(line); 
} 

これはEclipse IDE内で完璧に動作します。しかし、コマンドラインでjarファイルとして実行すると、何も印刷されません。

[email protected]65e

のreadLine()は、コマンドラインの実行中にnullを与えます。

誰でも助けてください。

+0

私はあなたがやろうとしていることに従うのは少し難しいと感じましたが、うまくいくはずです。おそらく、問題を再現する小さなサンプルプロジェクトを共有できますか? –

+0

@AndyWilkinson - 基本的に親JARファイルクラスから、私は依存関係jarのクラスパス内のプロパティファイルを読み込もうとしています。 – RedGuts

答えて

0

また、私の仕事です。あなたは異なるプロファイル(-Dspring.profiles.active)

@Configuration 
@ComponentScan("yourpackage") 
public class AppConfig { 
    @Profile("alpha") 
    @Configuration 
    @PropertySource("common-alpha.properties") 
    static class Alpha{} 

    @Profile("staging") 
    @Configuration 
    @PropertySource("common-staging.properties") 
    static class Staging{} 

    @Profile("production") 
    @Configuration 
    @PropertySource("common-production.properties") 
    static class Production{} 
} 

あなたは春に使用できることにより、設定ファイルを読みたい場合は

アプリプロジェクト

@Configuration 
@ComponentScan("yourpackage") 
public class AppConfig { 
    @Configuration 
    @PropertySource("common.properties") 
    static class default{} 
} 

でこのクラスを作成します@Autowiredアノテーションを以下のようにしますが、クラスに@Componentなどのアノテーションを付けるようにしてください。

@Autowired 
Environment env; 

あなたのプロパティでプロパティが、私はそれが役に立てば幸い

env.getProperty("property") 

を提出得ることができます。

+0

私は試してみることができますが、ハードコードされた静的ファイル – RedGuts

+0

@PropertySource( "$ {propertiesArgument} .properties")ではなく、プロパティのソースを引数パラメータにして-DpropertiesArgumentアプリケーション起動時に指定されたonコマンドラインパラメータ。 –

関連する問題