Java 未调用applicationContextProvider

Java 未调用applicationContextProvider,java,spring,Java,Spring,我正在使用Spring3.0.3 我想使用applicationContextProvider,因此我声明: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.sp

我正在使用Spring3.0.3

我想使用applicationContextProvider,因此我声明:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:tx="http://www.springframework.org/schema/tx"
     xsi:schemaLocation="
     http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <bean id="applicationContextProvider" class="com.mycompany.util.ApplicationContextProvider"></bean>
    <context:annotation-config/>
    <tx:annotation-driven/>
</beans>
但是这一套从来没有人叫过

每当我使用
ApplicationContextProvider时,getApplicationContext()返回null


为什么会这样?

部分问题可能是getter是静态的。因此,您可以在Spring创建ApplicationContextProvider实例之前调用它

当Spring“准备好”供您使用时,您需要引用Spring为您创建的bean“applicationContextProvider”。看

例如,使用src/test/resources中的“app context.xml”中的bean进行Junit测试

package com.mycompany.util;

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@ContextConfiguration(locations="classpath:app-context.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class ApplicationContextProviderTest {

    @Autowired // Injected by Spring when bean is "ready"
    ApplicationContextProvider contextProvider;


    @Test
    public void testContext() {
        assertNotNull(contextProvider);
        ApplicationContext context = ApplicationContextProvider.getApplicationContext();
        assertNotNull(context);

        System.out.println("My context has " + context.getBeanDefinitionCount() + " beans");
    }
}
然后,将为正在设置的applicationContext获取一个绿色条

示例输出(请不要在测试中遗漏System.out)

app-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="applicationContextProvider" class="com.mycompany.util.ApplicationContextProvider"></bean>


</beans>

看来您在这里做的事情不对。。。您想从不是由Spring管理的对象访问Springbean吗?那么:

但是
WebApplicationContextUtils
应该始终作为最后的手段,因为这不是Spring的方式。您确定不能以某种方式将web服务与Spring集成吗?例如,使用ApacheCXF,可以简单地将WS-endpoint实现为Springbean,或者将客户端代理注入到其他bean中


使用静态字段总是自找麻烦。我相信你可以以一种更优雅的方式实现你的目标。

这是一个已经被接受的答案的老问题,但对我来说,还不清楚为什么没有在
ApplicationContext提供程序上调用
setApplicationContext()
方法。答案提供了如何,但实际上没有提供为什么

Spring使用惰性策略创建bean:bean只会在第一次需要时创建。这就是为什么接受答案中的示例起作用的原因:bean是
ApplicationContextProviderTest
所需要的,因此它是在该点而不是之前创建的,并且
setApplicationContext()
方法在该时间点被调用,并且一切都按预期工作

然而,如果你的bean没有自动连接到任何地方,这一切都不会发生。在这种情况下,解决方案是告诉Spring不要使用延迟实例化,即:

<bean id="applicationContextProvider" lazy-init="false" class="com.mycompany.util.ApplicationContextProvider" />

这样,bean将在应用程序启动时创建,此时将调用方法
setApplicationContext()
。然后,您可以使用bean后缀,前提是该用法发生在应用程序启动之后


当然,更好的方法是将bean自动连接到任何需要的地方,但有时这是不可能的(请阅读:遗留应用程序)。

您确定在上下文完成初始化后调用
getApplicationContext()
?我如何知道它是在初始化之前还是之后?我是从Spring中的另一个bean调用它的。如果您的bean在启动过程中被实例化,那么它可能会在ApplicationContext注入到ApplicationContextProvider之前被实例化,并调用getApplicationContext“太快”。您需要提供程序做什么,不能直接将依赖项注入到使用上下文的bean中吗?为什么要使用静态
ApplicationContext
字段?如果您有使用上下文的代码,为什么不直接将其注入其中?即使在服务器启动后,它仍然是空的!对于使用SpringBean的web服务,我需要它。静态字段不是问题所在。它在bean生命周期的早期调用了accessor方法。这是一个很好的答案,但是
bean生命周期的链接现在已经死了。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="applicationContextProvider" class="com.mycompany.util.ApplicationContextProvider"></bean>


</beans>
WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
<bean id="applicationContextProvider" lazy-init="false" class="com.mycompany.util.ApplicationContextProvider" />