2012-04-30 44 views
0

我有使用RestEasy的(AppEngine上上運行),用下面的僞代碼REST服務:讀了「屬性」文件

@Path("/service") 
public class MyService { 
    @GET 
    @Path("/start") 
    public Response startService() { 
    // Need to read properties file here. 
    // like: servletContext.getResourceAsStream("/WEB-INF/config.properties") 
    } 
} 

但其明顯的servlet上下文無法訪問這裏。

和代碼,如:

InputStream inputStream = this.getClass().getClassLoader() 
       .getResourceAsStream("/WEB-INF/config.properties"); 

不能在AppEngine上的環境中執行。

編輯:

我試圖與春天做這樣的:

appContext.xml

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="locations" value="/WEB-INF/auth.properties"/> 
</bean> 

然後,把這個實際的類字段:

@Path("/service") 
public MyService{ 
    @Autowired 
    @Value("${myservice.userid}") 
    private String username; 
    @Autowired 
    @Value("${myservice.passwd}") 
    private String password; 
// Code omitted 
} 

然而,MyService的部分代碼抱怨,因爲usernamepassword沒有「注入」,我的意思是它的空,雖然其對auth.properties文件

回答

2

如果你把文件/WEB-INF/classes/(這應該工作其中,重要的,位於類路徑中),將config.properties指定爲頂級文件。

this.getClass().getClassLoader().getResourceAsStream("/config.properties"); 

看到這個類似的問題:How to load properties file in Google App Engine?

編輯:現在你已經編輯,我會迴應&答案春節相關的問題。因此,將auth.properties放入/ WEB-INF/classes /中,然後按如下所示指定classpath。

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location" value="classpath:auth.properties"/> 
</bean> 
+0

我不能把屬性在WEB-INF文件夾中的文件? – xybrek

+1

不,不是用這種方法,因爲WEB-INF不在類路徑中。該方法,'getResourceAsStream(..)'在類路徑中查找。如果你不知道如何將它放到你的類路徑中,你可以把它放在你的源文件夾中,讓'javac'發送給你。 – laher

+0

同樣的問題,用戶名和密碼沒有注入 – xybrek