2012-04-30 41 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