简体   繁体   English

使用getResourceAsStream在m子中注入文件

[英]Injecting file in mule using getResourceAsStream

I'm using Mule 3.4. 我正在使用Mule 3.4。 I kept a xml file in /src/main/resource/request and using initialize method and resource as stream, tried to load file from classpath and getting Null Pointer Exception 我在/ src / main / resource / request中保留了一个xml文件,并使用initialize方法和资源作为流,试图从classpath加载文件并获取Null Pointer Exception

My configuration as follows spring.xml 我的配置如下spring.xml

    <bean id="initSoapFault" class="org.hhmi.transformer.SOAPResponseTransformer" init-method="initialize" />

My Flow is following 我的流程正在关注

    <spring:beans>
     <spring:import resource="classpath:conf/spring.xml"/>
     </spring:beans>

My transformer class is code snippet is following 我的转换器类是下面的代码片段

    public void initialize() throws IOException {
     try {
       inputStream =Thread.currentThread().getContextClassLoader().getResourceAsStream("classpath:request/soapFault.xml");
       docXmlResponse = IOUtils.toString(inputStream);
        } finally {
       IOUtils.closeQuietly(inputStream);
        }       }

The problem is most likely that you use the " classpath: " prefix when invoking getResourceAsStream() . 问题很可能是在调用getResourceAsStream()时使用了“ classpath: ”前缀。 That prefix should only be used in conjunction with a Spring ResourceLoader , but not with a standard Java classloader . 该前缀只能与Spring ResourceLoader结合使用,而不能与标准Java classloader

IT's working. IT在工作。 I'm putting code snippet, it may help others 我正在编写代码段,这可能会对其他人有所帮助

to get bean in transformer. 让豆子进入变压器。 I use following 我使用以下

      private volatile ClassPathXmlApplicationContext context;

     public SOAPResponseTransformer()  {
      context = new ClassPathXmlApplicationContext(getConfigResources());
     }

 protected String getConfigResources()
{
    return "conf/spring.xml";
}

Then call where ever you need bean 然后在任何需要的地方打电话

  ResourceInjection injection =  (ResourceInjection) context.getBean("initSoapFault");

  injection.getter Method 

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

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