I have a jar which defines a bean like:
@Component(value="SHTTP")
public class SHTTPImpl{
}
Inside the jar , i have another class which loads the above defined bean like:
public static xx getInstance() {
getApplicationContext().getBean("SHTTP"); //Exception thrown here
}
where the function getApplicationContext() is defined as:
private static AnnotationConfigApplicationContext getApplicationContext() {
if (appContext == null) {
appContext = new AnnotationConfigApplicationContext(XXContext.class);
}
return appContext;
}
Now, I added this jar file inside another project ,say "project B" which has its own xml application context. I have a class inside project B which tries to get the bean "SHTTPImpl" by calling function
getInstance()
.
But, this doesnt seem to work as i get the org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'SHTTP' is defined
This problem occurs only when i run my testcase. IF i deploy my application in the server and run, the bean not found exception isnt happening. But, writing working testcase is important for me. So, please help me out
Thanks
EDIT : Here is my test case
@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);
}
}
EDIT 2: I think it would be better to add how i define the root of my context in the jar file. Here is the content of
XXContext.java file:
@Configuration
@ComponentScan(basePackages = { "com.xxx.adapter.sms" }) //SHTTPImpl is in package com.xxx.adapter.sms.syn
public class XXContext {
@Bean
...
}
The file test-service-context.xml in the project (this is not inside jar) defines a bunch of beans that isnt related to the issue in hand
EDIT 3 Adding test-service-context.xml content (only relevant parts)
<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>