1

我有一个 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”

获取实例()

.

但是,这似乎不起作用,因为我得到了 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'SHTTP' is defined

仅当我运行我的测试用例时才会出现此问题。如果我在服务器中部署我的应用程序并运行,则不会发生 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 文件:

  @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>
4

0 回答 0