2014-10-01 25 views
0

雖然我看到很多問題,但有些答案與我的設置無關。 我有3個模塊的API,Impl和消費者。消費者依賴於API和Impl。消費者擁有的web.xml看起來像調用spring上下文的Java模塊拋出BeanFactory未初始化或已關閉 - 在訪問bean之前調用'refresh'

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

在消費資源的conf /春-config.xml中與配置

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

的repository.xml內我有

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

我失去了存在這裏有什麼?

整個堆棧跟蹤是如下

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) 

由於事先 任何幫助表示讚賞

+0

你在哪裏聲明你的bean?你在使用@repository註釋嗎? – codeMan 2014-10-01 09:15:36

+0

請顯示完整的堆棧跟蹤。 – Jens 2014-10-01 09:15:49

+0

這就是正確的Impl我正在使用@Repository註釋。 – Raghuveer 2014-10-01 09:39:49

回答

0

Oups,你永遠不應該寫類似

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

正如你在上次顯示評論!您創建一個新的空的和未初始化的應用程序上下文,而不是使用spring ContextLoaderListener創建的應用程序上下文。

Spring允許你自動或明確地連線bean,但你自己的類不應該直接訪問應用程序上下文,除非是非常特殊的用例。它應該是足夠寫(只是猜測,因爲我不知道你的配置,但它是一個例子):

@Autowired 
MongoRepository mongoRepository; 

如果配置包含MongoRepository類型的只有一個豆,如果TestMe也有春天豆,那麼彈簧會自動他們。如果你有相同類型的多個豆,你可以用@Qualifier註解指定的名稱:

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

當然,你也可以定義在XML配置。

相關問題