我有使用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
的部分代码抱怨,因为username
和password
没有“注入”,我的意思是它的空,虽然其对auth.properties
文件
我不能把属性在WEB-INF文件夹中的文件? – xybrek
不,不是用这种方法,因为WEB-INF不在类路径中。该方法,'getResourceAsStream(..)'在类路径中查找。如果你不知道如何将它放到你的类路径中,你可以把它放在你的源文件夹中,让'javac'发送给你。 – laher
同样的问题,用户名和密码没有注入 – xybrek