2011-07-28 4 views
0

シンプルなように見えますが、それを把握することはできません。<jaxws:client>タグの相対アドレス属性を設定する方法はありますか?

複数の場所(異なるサーブレットコンテナ、異なるポート)にデプロイできるように、CXFでSpring MVC Webアプリケーションを設定する必要があります。すべて正常に動作しますが、JAX-WSクライアントをコンフィグレーションするCXFのXMLコンフィグレーションでは、タグには指定された絶対URLを持つ「address」属性が必要です。ここで

は、コードは次のとおりです。

<jaxws:client id="wsClient" serviceClass="com.foo.WebServiceInterface" address="http://localhost:8092/WSApp/WebServiceImplPort" />  

は私の目標を達成するために相対的にアドレスや他のシンプルなソリューションを変更する方法はありますか?ありがとうございました!

答えて

0

mavenを(あるいはおそらくantでも)デプロイメントに使用する場合は、xmlファイルでプロパティを使用し、pomのリソースフィルタリングを有効にして各環境でこのパラメータを設定できます。

<build> 
    <resources> 
     <resource> 
      <directory>src/main/resources</directory> 
     </resource> 
    </resources> 
    <!-- Turn on filtering of test resources to allow the correct environment for unit tests --> 
    <testResources> 
     <testResource> 
      <directory>src/test/resources</directory> 
      <filtering>true</filtering> 
     </testResource> 
    </testResources> 
</build> 

実際にクライアント参照を作成する際には、クライアント参照を作成するときにURLを渡すためのオプションがあります。

SomeServiceClientEP client = new SomeServiceClientEP(url, qname); 
dynamicallySetEndpoint((BindingProvider) SomeService, destinationURL); 

/** 
* Change the destination endpoint url for some service to the 
* provided in the <i>destinationURL</i> parameter 
* 
* @param service 
*   The {@link SomeService} JAXWS proxy for accessing the 
*   service 
* @param destinationURL 
*   The URL of the SomeService to send the 
*   request to. If the URL contains ?wsdl at the end it will be 
*   stripped prior to attempting delivery 
*/ 
protected void dynamicallySetEndpoint(BindingProvider service, final String destinationURL) { 
    Map<String,Object> rc = service.getRequestContext(); 
    // Strip the ?wsdl off the end of the URL 
    String url = destinationURL; 
    if (destinationURL.toLowerCase().matches(WsdlSuffixPattern)) { 
     url = destinationURL.substring(0, destinationURL.length() - WsdlSuffix.length()); 
    } 
    rc.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, url); 
} 
0

設定を配置する場所は議論の余地があり、好みによって異なります。 Mavenリソースフィルタに依存するためには、すべての展開で戦争を再度パッケージ化する必要があります。私にとって、それは選択肢ではありません。あなたの展開設定に応じて異なる春のプロファイルを定義し、例えば、残りのサービスが展開されているプロパティファイルに定義することができ

オプション1つの春プロファイル

<beans profile="develop"> 
    <context:property-placeholder location="classpath:develop-config.properties" /> 
</beans> 

<beans profile="production"> 
    <context:property-placeholder location="classpath:production-config.properties" /> 
</beans> 

<jaxws:client id="wsClient" serviceClass="com.foo.WebServiceInterface" address="${address}" /> 

production-config.propertiesが持っている可能性があります。アクティブお気に入りのプロファイルに

address=http://localhost:8092/WSApp/WebServiceImplPort 

を:-Dspring.profiles.active =生産

オプション2春のenviromentsを使用してアプリケーションを起動し

Springでは、有効またはアクティブなプロファイルp文法的に。これは、すべての環境に異なる設定ファイルを読み込むために使用できます。あるいは、あなたのスプリングコンテキストのjavaシステム変数または有効なシステム環境変数を利用できるようにすることができます。 設定についてに従ってください。彼らは、設定は環境変数に置くべきだと言います。 またはむしろ1戦争より多くの展開アプローチを好む。これはあなたの選択です。あなたのWebに追加することを忘れないでください

import org.springframework.context.ApplicationContextInitializer; 
import org.springframework.core.env.ConfigurableEnvironment; 
import org.springframework.core.env.MapPropertySource; 
import org.springframework.core.io.support.ResourcePropertySource; 
import org.springframework.web.context.ConfigurableWebApplicationContext; 

public class EnviromentDiscovery implements org.springframework.context.ApplicationContextInitializer.ApplicationContextInitializer<org.springframework.context.ApplicationContextInitializer.ConfigurableWebApplicationContext> { 

    public void initialize(ConfigurableWebApplicationContext ctx) { 
     ConfigurableEnvironment environment = ctx.getEnvironment();  
     // This variable has the same meaning as host, but i can be redefined in 
     // case of cluster deployment. 
     String hostname= InetAddress.getLocalHost().getHostName(); 
     logger.info("Application deployed int host {} using context path: {}", engineName, contextPath); 
     // You should define a method which load your config.properties according your own criteria depending for example on your hostname. 
     InputStream configurationSource = getResourceAsStream(hostname); 
     Properties config = new Properties(); 
     config.load(configurationSource);   
     // Take your address endpoint 
     String address = config.getProperty("address"); 
     Map<String, Object> props = new HashMap<>(); 
     props.put("address", address); 

     MapPropertySource mapSource = new MapPropertySource("props", props); 
     // Voilá! your property address is available under spring context! 
     environment.getPropertySources().addLast(mapSource); 
     ctx.registerShutdownHook(); 
    } 
} 

今すぐあなたのコンテキストファイルは次のようになります。XML

<context-param> 
    <param-name>contextInitializerClasses</param-name> 
    <param-value>net.sf.gazpachoquest.bootstrap.EnviromentDiscovery</param-value> 
    </context-param> 

詳細情報についてSpring environment here:またはあなたがmy proof of concept hereをチェックアウトしたい場合。

関連する問題