使用Jboss7,Load Resource返回null [英] Load Resource returns null with Jboss7

查看:201
本文介绍了使用Jboss7,Load Resource返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Jboss7.1从java代码加载图像等资源?



这曾经用于Jboss4:

  this.getClass()。getClassLoader()。getResourceAsStream(/ myapp / includes / images / image1.png); 

现在返回null。



什么现在使用Jboss7加载java代码中的资源是最佳做法吗?



我做了一些测试:

  URL url = this.getClass()。getResource(); 
System.out.println(url);
url = this.getClass()。getResource(../../../);
System.out.println(url);
url = this.getClass()。getResource(../../../../);
System.out.println(url);
url = this.getClass()。getResource(../../../../../);
System.out.println(url);
url = this.getClass()。getResource(includes);
System.out.println(url);

13:33:49,143 INFO [stdout](http - 127.0.0.1-8080-1)vfs:/ C:/Eclipse/apps/jboss-as-7.1.1.Final/standalone /deployments/my-ea.ear/my-web.war/WEB-INF/classes/com/xxx/yyy/beans/jsf/
13:33:49,144 INFO [stdout](http - 127.0。 0.1-8080-1)vfs:/ C:/Eclipse/apps/jboss-as-7.1.1.Final/standalone/deployments/my-ea.ear/my-web.war/WEB-INF/classes/com/ xxx /
13:33:49,150 INFO [stdout](http - 127.0.0.1-8080-1)jar:file:/ C:/Eclipse/apps/jboss-as-7.1.1.Final/modules /javax/activation/api/main/activation-1.1.1.jar!/com/
13:33:49,151 INFO [stdout](http - 127.0.0.1-8080-1)文件:/ C: /Eclipse/apps/jboss-as-7.1.1.Final/modules/sun/jdk/main/service-loader-resources/
13:33:49,152 INFO [stdout](http - 127.0.0.1- 8080-1)null


解决方案

as 记录(不使用 jboss-deployment-structure.xml file):


  1. 为配置文件创建一个模块( jboss-as-7 / modules /com/yourcompany/configuration/main/module.xml ):



 <?xml version =1.0encoding =UTF-8?> 
< module xmlns =urn:jboss:module:1.1name =com.mycompany.configuration>
< resources>
< resource-root path =。/>
< / resources>
< / module>




  1. 向模块添加属性:



  jboss-as-7 / 
modules /
com /
yourcompany /
configuration /
main /
module.xml
settings.properties
other-settings.xm




  1. 将模块添加到 CLASSPATH 使用 MANIFEST.MF 条目:



 清单 - 版本:1.0 
依赖关系:com.mycompany.configuration




  1. 从CLASSPATH加载属性文件:



< pre class =lang-java prettyprint-override> InputStream settingsStream =
getClass()。getClassLoader()。getResourceAsStream(settings.properties);

我必须创建一个名为 com / mycompany / main的模块文件夹并添加所有图像。因此,至少在 JBOSS_HOME 中,它位于WAR之外。然后我可以使用以下方法加载图像:

  URL imgUrl = this.getClass()。getClassLoader()。getResource(myimage。 JPG); 


How do I load resources like images from the java code with Jboss7.1?

This used to work with Jboss4:

this.getClass().getClassLoader().getResourceAsStream("/myapp/includes/images/image1.png");

Now this returns null.

What is best practice for loading resources in java code now with Jboss7?

I did some testing:

URL url = this.getClass().getResource("");
System.out.println(url);
url = this.getClass().getResource("../../../");
System.out.println(url);
url = this.getClass().getResource("../../../../");
System.out.println(url);
url = this.getClass().getResource("../../../../../");
System.out.println(url);
url = this.getClass().getResource("includes");
System.out.println(url);

13:33:49,143 INFO  [stdout] (http--127.0.0.1-8080-1) vfs:/C:/Eclipse/apps/jboss-as-7.1.1.Final/standalone/deployments/my-ea.ear/my-web.war/WEB-INF/classes/com/xxx/yyy/beans/jsf/
13:33:49,144 INFO  [stdout] (http--127.0.0.1-8080-1) vfs:/C:/Eclipse/apps/jboss-as-7.1.1.Final/standalone/deployments/my-ea.ear/my-web.war/WEB-INF/classes/com/xxx/
13:33:49,150 INFO  [stdout] (http--127.0.0.1-8080-1) jar:file:/C:/Eclipse/apps/jboss-as-7.1.1.Final/modules/javax/activation/api/main/activation-1.1.1.jar!/com/
13:33:49,151 INFO  [stdout] (http--127.0.0.1-8080-1) file:/C:/Eclipse/apps/jboss-as-7.1.1.Final/modules/sun/jdk/main/service-loader-resources/
13:33:49,152 INFO  [stdout] (http--127.0.0.1-8080-1) null

解决方案

As documented on the JBoss community wiki (without using a jboss-deployment-structure.xml file):

  1. Create a module for the configuration file (jboss-as-7/modules/com/yourcompany/configuration/main/module.xml):

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.mycompany.configuration">
    <resources>
        <resource-root path="."/>
    </resources>
</module>

  1. Add properties to the module:

jboss-as-7/
   modules/
      com/
         yourcompany/
            configuration/
               main/
                 module.xml
                 settings.properties
                 other-settings.xm

  1. Add the module to the CLASSPATH using a MANIFEST.MF entry:

Manifest-Version: 1.0
Dependencies: com.mycompany.configuration

  1. Load a properties file from the CLASSPATH:

InputStream settingsStream = 
  getClass().getClassLoader().getResourceAsStream("settings.properties");

I had to create a module folder called com/mycompany/main and add all the images in there. So this sits outside the WAR at least though within JBOSS_HOME. Then I could load the image using:

URL imgUrl = this.getClass().getClassLoader().getResource("myimage.jpg");

这篇关于使用Jboss7,Load Resource返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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