我有一个jar,它定义了一个像:
的bean@Component(value="SHTTP")
public class SHTTPImpl{
}
在jar中,我有另一个类加载上面定义的bean,如:
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文件,比如“项目B”,它有自己的xml应用程序上下文。我在项目B中有一个类,它通过调用函数
来尝试获取bean“SHTTPImpl”的getInstance()
但是,这似乎不起作用,因为我得到了org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为'SHTTP'的bean
仅当我运行我的测试用例时才会出现此问题。如果我在服务器中部署我的应用程序并运行,则找不到bean找不到异常。但是,编写工作测试用例对我来说非常重要。所以,请帮帮我
由于
编辑:这是我的测试用例
@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文件:
@Configuration
@ComponentScan(basePackages = { "com.xxx.adapter.sms" }) //SHTTPImpl is in package com.xxx.adapter.sms.syn
public class XXContext {
@Bean
...
}
项目中的文件test-service-context.xml(这不在jar中)定义了一堆与手头的问题无关的bean
编辑3 添加test-service-context.xml内容(仅限相关部分)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<bean id="processTransactions" class="com.XXX.batch.listener.ProcessIncomingTransactionsNoThreads" >
<property name="batchDetailDAO" ref="batchDetailDAO"/>
<property name="batchHeaderDAO" ref="batchHeaderDAO"/>
<property name="batchTXDetailDAO" ref="batchTXDetailDAO"/>
<property name="batchServiceUtil" ref="batchServiceUtil"/>
<property name="batchMessageUtil" ref="batchMessageUtil"/>
</bean>
<bean id="mBankingSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.XXX.batch.common.model.BatchControlParam</value>
<value>com.XXX.batch.common.model.BatchDetail</value>
<value>com.XXX.batch.common.model.BatchFileStatusLookup</value>
<value>com.XXX.batch.common.model.BatchHeader</value>
<value>com.XXX.batch.common.model.BatchPartnerPriority</value>
<value>com.XXX.batch.common.model.BatchTXDetail</value>
<value>com.XXX.batch.common.model.BatchTXStatusLookup</value>
<value>com.XXX.batch.common.model.Party</value>
<value>com.XXX.batch.common.model.PartyAttributes</value>
<value>com.XXX.batch.common.model.PartyAttributeValues</value>
<value>com.XXX.batch.common.model.PartyType</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.jdbc.batch_size">10</prop>
</props>
</property>
</bean>