读取“属性”文件 [英] Reading a "properties" file

查看:147
本文介绍了读取“属性”文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Rest服务,使用Resteasy(运行在Appengine之上),使用以下伪代码:

  @Path (/ service)
public class MyService {
@GET
@Path(/ start)
public response startService(){
//需要在这里读取属性文件。
// like:servletContext.getResourceAsStream(/ WEB-INF / config.properties)
}
}

然而很明显,servlet上下文无法在这里访问。



代码如下:

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

无法在Appengine环境中执行。



编辑:



p>

appContext.xml

 < bean类= org.springframework.beans.factory.config.PropertyPlaceholderConfigurer > 
< property name =locationsvalue =/ WEB-INF / auth.properties/>
< / bean>

然后,将其放在实际的类字段中:

  @Path(/ service)
public MyService {
@Autowired
@Value($ {myservice.userid})
私人字符串用户名;
@Autowired
@Value($ {myservice.passwd})
私人字符串密码;
//代码省略
}

然而,部分代码 MyService 抱怨,因为用户名密码未被注入,我的意思是它是空的,尽管它在 auth.properties 文件中

解释方案

在RESTEasy中,您可以通过@Context注释轻松注入Servlet上下文: http://docs.jboss.org/resteasy/docs/2.3.1.GA/userguide/html_single/index.html#_Context



可以在这里找到示例:轻松实现并初始化参数 - 如何访问?


I have a Rest service using Resteasy (running on top of Appengine), with the following psuedo code:

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

However its obvious that the servlet context cannot be accessed here.

And code like:

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

Can't be executed within the Appengine environment.

EDIT:

I have tried doing it with Spring like:

appContext.xml

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

Then, put this on the actual class fields:

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

However, part of the code of the MyService complains because the username and password was not "injected", I mean its empty although its on the auth.properties file

解决方案

In RESTEasy you can easily inject Servlet context via @Context annotation: http://docs.jboss.org/resteasy/docs/2.3.1.GA/userguide/html_single/index.html#_Context

Examples can be found here: Rest easy and init params - how to access?

这篇关于读取“属性”文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆