0

All though I see lots of questions, some answers are not relevant to my setup. I have a total of 3 modules API, Impl and consumer. Consumer has dependency of API and Impl. Consumer has web.xml, which looks like

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath*:/**/conf/spring-config.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>HelloWorld</servlet-name>
        <servlet-class>com.name.TestMe</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloWorld</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</webapp>

In consumer resource conf/spring-config.xml exists with configuration

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-4.1.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

<import resource="classpath*:/**/repository.xml"/>
</beans>

within repository.xml I have

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-4.1.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:/**/properties/mongo-config.properties
                </value>
            </list>
        </property>
    </bean>

    <!-- <import resource="config/mongo-core.xml" /> -->
    <import resource="config/spring-core.xml" />

</beans>

Am I missing anything here?

The whole stacktrace is as below

java.lang.IllegalStateException: BeanFactory not initialized or already closed - call 'refresh' before accessing beans via the ApplicationContext
    at org.springframework.context.support.AbstractRefreshableApplicationContext.getBeanFactory(AbstractRefreshableApplicationContext.java:170)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
    at com.name.TestMe.doGet(TestMe.java:31)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
halfer
  • 19,824
  • 17
  • 99
  • 186
Raghuveer
  • 2,859
  • 7
  • 34
  • 66

1 Answers1

0

Oups, you should never write something like

ApplicationContext context = new ClassPathXmlApplicationContext();
mongoRepository = (MongoRepository)context.getBean("mongoRepositoryImpl");

as you showed in you last comment ! You create a new empty and uninitialized application context, instead of using the one created by spring ContextLoaderListener.

Spring allows you to wire beans, either automatically or explicitely, but your own classes should not directly access the application context except for very special use cases. It should be enough to write (just a guess because I do not know your config, but it is an example) :

@Autowired
MongoRepository mongoRepository;

If you configuration contains exactly one bean of type MongoRepository, and if TestMe is also a spring bean, then spring will automatically wire them. If you have more than one bean of same type, you can specify the name with @Qualifier annotation :

@Autowired
@Qualifier("mongoRepositoryImpl")
MongoRepository mongoRepository;

Of course, you can also define that in xml config.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252