在将XML文件作为(Spring Boot Application)jar文件的一部分加载时,未从spring-context.xml识别Bean

时间:2016-01-05 11:56:45

标签: java xml spring spring-boot spring-integration

我有一个任务,其中在使用Spring Boot创建jar文件后,我必须使用Java类中的URLClassLoader加载它来执行它。问题是虽然直接启动时jar文件运行正常,但是当我尝试使用以下代码在Java类中加载它时,它会失败。

public static void main(String[] args) 
{       
        // Getting the jar URL which contains target class
        URL[] classLoaderUrls = new URL[]{new URL("file:///C://Users\\CHH7KOR\\Documents\\POC\\BootDemo\\target\\BootDemo-0.0.1-SNAPSHOT.jar")};

        // Create a new URLClassLoader 
        URLClassLoader urlClassLoader = new URLClassLoader(classLoaderUrls);

        Thread.currentThread().setContextClassLoader(urlClassLoader);
            System.out.println("ASHD:"+Thread.currentThread().getContextClassLoader());
        // Load the target class
        Class<?> beanClass = urlClassLoader.loadClass("sample.integration.SampleIntegrationApplication");

        // Create a new instance from the loaded class
        Constructor<?> constructor = beanClass.getConstructor();
        Object beanObj = constructor.newInstance();           

        // Getting a method from the loaded class and invoke it
        Method method = beanClass.getMethod("doAllFunctions");
        method.invoke(beanObj);         
}

我运行时遇到的错误是:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at Sample.main(Sample.java:33)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'ftpInboundChannel' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:698)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1175)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1060)
    at sample.integration.SampleIntegrationApplication.doAllFunctions(SampleIntegrationApplication.java:57)
    ... 5 more

此处ftpInboundChannel是配置为spring-config.xml

的一部分的频道

0 个答案:

没有答案