0

尽管我看到很多问题,但有些答案与我的设置无关。我一共有 3 个模块 API,Impl 和 consumer。Consumer 依赖于 API 和 Impl。消费者有 web.xml,看起来像

<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/spring-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)
4

1 回答 1

0

哎呀,你永远不应该写类似的东西

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

正如你在上一条评论中所展示的!您创建一个新的空且未初始化的应用程序上下文,而不是使用 spring 创建的上下文ContextLoaderListener

Spring 允许您自动或显式地连接 bean,但您自己的类不应直接访问应用程序上下文,除非非常特殊的用例。写起来应该足够了(只是一个猜测,因为我不知道你的配置,但这是一个例子):

@Autowired
MongoRepository mongoRepository;

如果你的配置只包含一个 bean 类型MongoRepository,并且TestMe也是一个 spring bean,那么 spring 会自动连接它们。如果您有多个相同类型的 bean,则可以使用@Qualifierannotation 指定名称:

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

当然,你也可以在 xml config 中定义。

于 2014-10-01T12:34:22.103 回答