Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring无法从jar文件加载bean_Java_Spring_Junit - Fatal编程技术网

Java Spring无法从jar文件加载bean

Java Spring无法从jar文件加载bean,java,spring,junit,Java,Spring,Junit,我有一个jar,它定义了一个bean,比如: @Component(value="SHTTP") public class SHTTPImpl{ } public static xx getInstance() { getApplicationContext().getBean("SHTTP"); //Exception thrown here } 在jar中,我有另一个类,它加载上面定义的bean,如: @Component(value="SHTTP") public clas

我有一个jar,它定义了一个bean,比如:

@Component(value="SHTTP")
public class SHTTPImpl{
}   
public static xx getInstance() {
   getApplicationContext().getBean("SHTTP"); //Exception thrown here
}
在jar中,我有另一个类,它加载上面定义的bean,如:

@Component(value="SHTTP")
public class SHTTPImpl{
}   
public static xx getInstance() {
   getApplicationContext().getBean("SHTTP"); //Exception thrown here
}
其中,函数getApplicationContext()定义为:

private static AnnotationConfigApplicationContext getApplicationContext() {
    if (appContext == null) {
        appContext = new AnnotationConfigApplicationContext(XXContext.class);
    }
    return appContext;
}
现在,我在另一个项目中添加了这个jar文件,比如“ProjectB”,它有自己的xml应用程序上下文。我在项目B中有一个类,它试图通过调用函数来获取bean“SHTTPImpl”

getInstance()

但是,当我得到org.springframework.beans.factory.NoSuchBeanDefinitionException时,这似乎不起作用:没有定义名为“SHTTP”的bean

只有在运行测试用例时,才会出现此问题。如果我在服务器中部署应用程序并运行,则不会发生bean not found异常。但是,编写工作测试用例对我来说很重要。所以,请帮帮我

谢谢

编辑:这是我的测试用例

@Test
public void testprocessRequestMessageType() throws Exception{
    ProcessIncomingTransactionsNoThreads processIncomingTransactionNoThreads = (ProcessIncomingTransactionsNoThreads)getBean("processTransactions");
    processIncomingTransactionNoThreads.processRequestMessageType(cm); //This method tries to load the bean from the jar file
}

public class BaseTest extends TestCase {
public static ApplicationContext getContext() {
    return new FileSystemXmlApplicationContext("/src/test/resources/test-service-context.xml");
}

public static Object getBean(String beanName) {
    return getContext().getBean(beanName);
}   
}
编辑2: 我认为最好在jar文件中添加如何定义上下文的根。这是本书的内容

XXContext.java文件:

项目中的文件test-service-context.xml(它不在jar中)定义了一组与当前问题无关的bean

编辑3添加test-service-context.xml内容(仅相关部分)


com.XXX.batch.common.model.BatchControlParam
com.XXX.batch.common.model.BatchDetail
com.XXX.batch.common.model.BatchFileStatusLookup
com.XXX.batch.common.model.BatchHeader
com.XXX.batch.common.model.BatchPartnerPriority
com.XXX.batch.common.model.BatchTXDetail
com.XXX.batch.common.model.BatchTXStatusLookup
com.XXX.batch.common.model.Party
com.XXX.batch.common.model.PartyAttributes
com.XXX.batch.common.model.PartyAttributeValues
com.XXX.batch.common.model.PartyType
org.hibernate.dialen.oracle10galent
真的
10

您是否对包含
SHTTPImpl
的包进行了组件扫描?@SotiriosDelimanolis是的,我进行了扫描您可以显示您的测试用例吗?您可能没有使用正确的配置类/文件。它非常大,但我将添加一部分。您可以发布test-service-context.xml的内容吗?