2016-12-01 24 views
3

Java DSLとMainオブジェクトを使用してプロパティファイルの使用を設定するにはどうすればよいですか?それは単に動作しません。しかしCamel読み込みプロパティファイル

main.setPropertyPlaceholderLocations("example.properties"); 

this pageによると、私のような何かを呼び出すことができるはずです。 Camel 2.18と2.17.1を実行するまでオプションが追加されていないようです。

アプリケーションをスタンドアロン形式で実行するときに使用するプロパティファイルを設定する元の方法は何でしたか?

いくつかの裏話:私は春からJava DSLに変換しようとしている

。その変換の間、私はCamelアプリケーションを単独で実行しようとしていました。私はそれがmain.run();を使用して達成されることを知っています。

CamelContextを使用しているときに "機能していた"機能がありましたが、それは単独では実行できません。だから私は、その場合には動作します以下を使用して知っている:

PropertiesComponent pc = new PropertiesComponent(); 
pc.setLocation("classpath:/myProperties.properties"); 
context.addComponent("properties", pc); 

私はその設定を使用するようにmainを伝えることができるいくつかの方法がありますか?それとも他に必要なことがありますか?

答えて

1

次のスニペットを使用することができます。

また
PropertiesComponent pc = new PropertiesComponent(); 
pc.setLocation("classpath:/myProperties.properties"); 
main.getCamelContexts().get(0).addComponent("properties", pc); 

、あなたがcamel-springを使用している場合、あなたはorg.apache.camel.spring.Mainクラスを使用することができ、それはあなたのアプリケーションコンテキストからプロパティプレースホルダを使用する必要があります。

+1

ああ、短い甘いとポイントに。ありがとう!あなたはそれが少し洗練されていると思います。しかし私はそれがCamel 2.18で新しい方法を導入した理由だと思います! – Jsmith

+0

Javaの設定に移行する場合は、Spring Bootに[Camelはそれを大きくサポートしています](https://camel.apache.org/spring-boot.html)を試してみてください。多くの定型文が削除されます。 –

1

あなたがSpring XMLからJava Configに移行する過程を指摘しているので、ここではプロパティを使用してCamelルートに注入する最小限のアプリケーションがあります(Springのプロパティ管理はCamelルートBeanに注入されます) :

my.properties

something=hey! 

メインクラス

パッケージCA melspringjavaconfig;

import org.apache.camel.spring.javaconfig.CamelConfiguration; 
import org.apache.camel.spring.javaconfig.Main; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.context.annotation.PropertySource; 

@Configuration 
@ComponentScan("camelspringjavaconfig") 
@PropertySource("classpath:my.properties") 
public class MyApplication extends CamelConfiguration { 

    public static void main(String... args) throws Exception { 
     Main main = new Main(); 
     main.setConfigClass(MyApplication.class); // <-- passing to the Camel Main the class serving as our @Configuration context 
     main.run(); // <-- never teminates 
    } 
} 

MyRouteクラス

package camelspringjavaconfig; 

import org.apache.camel.builder.RouteBuilder; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.core.env.Environment; 
import org.springframework.stereotype.Component; 

@Component 
public class MyRoute extends RouteBuilder { 

    @Autowired 
    Environment env; //<-- we are wiring the Spring Env 

    @Override 
    public void configure() throws Exception { 

     System.out.println(env.getProperty("something")); //<-- so that we can extract our property 

     from("file://target/inbox") 
       .to("file://target/outbox"); 
    } 
} 
+0

これはきちんとしていますが、アノテーションを使って設定できることを理解できませんでした。私はCamelのページに少しクリーナーの流れがあることを願っていますので、これらのもののいくつかを見つけることができました – Jsmith

関連する問題