【发布时间】:2012-04-30 07:19:04
【问题描述】:
我有一个使用 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 环境中执行。
编辑:
我试过用 Spring 来做:
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 的部分代码抱怨因为 username 和 password 没有“注入”,我的意思是它是空的,尽管它在 auth.properties 文件中
【问题讨论】:
标签: spring google-app-engine rest resteasy