简体   繁体   English

如何从EJB中获取Web资源的路径?

[英]How to get path to web resource from within EJB?

This is my first post on StackOverflow, so let me know if I'm breaking any rules. 这是我在StackOverflow上的第一篇文章,所以请让我知道我是否违反任何规则。

As for the question: 至于这个问题:

I have a JBOSS project divided as follows: 我有一个JBOSS项目,划分如下:

myproject
  -myproject-ear
  -myproject-ejb
     -mypackage
       -myQuartzClass.java
  -myproject-web
     -src
        -main
          -webapp
             -protected
                -sql
                  -mySqlQuery.sql

Thing is, I need to get the path (and then read the file) to "mySqlQuery.sql") from within the "MyQuartzClass.java". 问题是,我需要从“ MyQuartzClass.java”中获取“ mySqlQuery.sql”的路径(然后读取文件)。 Is it possible? 可能吗? I know I can use FacesContext.getCurrentInstance().getExternalContext().getRealPath from within a ManagedBean, but can I use something similar from within my EJB classes? 我知道我可以在ManagedBean中使用FacesContext.getCurrentInstance()。getExternalContext()。getRealPath,但是可以在EJB类中使用类似的东西吗?

Thanks 谢谢

At least for WAR deployments, you can have a ContextListener load some information into a static field of a non-@Startup-annotated EJB. 至少对于WAR部署,您可以让ContextListener将一些信息加载到非@Startup注释的EJB的静态字段中。 There's a code sample at https://github.com/IanDarwin/GetWebFilesInEJB.git https://github.com/IanDarwin/GetWebFilesInEJB.git上有一个代码示例

I have not tested it on the legacy EAR deployment; 我尚未在旧版EAR部署上对其进行测试; if it doesn't work there, might give some people the incentive to simplify their lives a bit :-) 如果在那儿行不通,可能会激励一些人简化生活:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM