我有使用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