2017-04-04 30 views
1

基於春季配置文件發佈澤西休息服務是可能的嗎? 可以這麼說如下例子,在使用profile1時如何發佈RegisterServices1基於春季配置文件的澤西休息服務發佈

public class ApiGWRestApplicationConfig extends ResourceConfig { 

    public ApiGWRestApplicationConfig() {  
     register(RegisterServicesApiGWInterface.class); 
    } 
} 

@Service 
@Profile("profile1") 
@Path(SystemConstants.REST_REGISTER) 
public class RegisterServices1 implements RegisterServicesApiGWInterface { 

}

@Service 
@Profile("profile2") 
@Path(SystemConstants.REST_REGISTER) 
public class RegisterServices2 implements RegisterServicesApiGWInterface{} 

的web.xml

<servlet> 
    <servlet-name>jersey-servlet-kagw</servlet-name> 
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
    <init-param> 
     <param-name>javax.ws.rs.Application</param-name> 
     <param-value>com.ttech.tims.imos.web.ApiGWRestApplicationConfig</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
+0

也許你可以將ApplicationContext注入到ResourceConfig中,並[[獲取所有使用@Path註釋的bean](http://stackoverflow.com/q/14236424/2587435)。你可以用'register'註冊它們。只是一個想法。我從來沒有嘗試過 –

+0

實際上,它不起作用,因爲ResourceConfig在web.xml中初始化,在彈出的上下文初始化過程中出現 –

+0

用[this project](https://github.com/psamsotha/jersey-彈簧實例/樹/主/ SRC /主/ JAVA/COM /失敗者/球衣/彈簧/實例)。沒有web.xml。這都是編程的,甚至SpringInitializer的彈簧配置。 ResourceConfig自動從@ApplicationPath註釋中獲取。如果它不適合你,我會再試一次 –

回答

1

那麼你可以做的就是讓ApplicationContext的持有和使用getBeansWithAnnotation(Path.class)。這將爲您提供屬於配置文件一部分的所有資源實例。然後你可以註冊這些實例。

我雖然這將是可能的ApplicationContext注入ResourceConfig,但如上面提到的the comment,似乎ResourceConfig的創作並沒有獲得它。

我能夠上班的,就是使用JAX-RS Feature,它也有權訪問註冊方法,就像您在ResourceConfig中那樣。使用Feature會給你訪問ApplicationContext

public class SpringProfilesFeature implements Feature { 

    @Inject 
    private ApplicationContext context; 

    @Override 
    public boolean configure(FeatureContext featureContext) { 
     Map<String, Object> resources = context.getBeansWithAnnotation(Path.class); 

     resources.values().forEach(resource -> featureContext.register(resource)); 

     return true; 
    } 
} 

然後,只需註冊與ResourceConfig

public AppConfig() { 
    register(SpringProfilesFeature.class); 
} 

功能刪除您有任何其他登記的所有資源。讓功能註冊它們。

我已經證實,這個工程。不知道你如何設定你的環境檔案,但希望這是你已經知道該怎麼做。

+0

我會盡力分享我的經驗,謝謝。 –