ApplicationContextProvider not called

I am using Spring 3.0.3.

I would like to use applicationContextProvider, so I stated:

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

and my ApplicationContextProvider:

public class ApplicationContextProvider implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
    public void setApplicationContext(ApplicationContext _applicationContext) throws BeansException {
        applicationContext = _applicationContext;

    }

}

But dialing is never called!

and whenever I use ApplicationContextProvider.getApplicationContext()returns null.

why?

+3
source share
3 answers

Part of the problem may be that your getter is static. That way you can call it before Spring by creating an instance of ApplicationContextProvider.

You need to refer to the bean 'applicationContextProvider' that Spring created for you when Spring is ready for you. See Bean Life Cycle

eg. via Junit test with your bean in 'app-context.xml' in src / test / resources

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 btw).

INFO : org.springframework.test.context.TestContextManager - @TestExecutionListeners is not present for class [class com.mycompany.util.ApplicationContextProviderTest]: using defaults.
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [app-context.xml]
INFO : org.springframework.context.support.GenericApplicationContext - Refreshing org.springframework.context.support.GenericApplicationContext@4c331059: startup date [Sun Feb 27 13:38:13 GMT 2011]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4b1c2b67: defining beans [applicationContextProvider,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy
My context has 5 beans
INFO : org.springframework.context.support.GenericApplicationContext - Closing org.springframework.context.support.GenericApplicationContext@4c331059: startup date [Sun Feb 27 13:38:13 GMT 2011]; root of context hierarchy
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@4b1c2b67: defining beans [applicationContextProvider,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy

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

, - . Spring beans , Spring? WebApplicationContextUtils:

WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

WebApplicationContextUtils , Spring. , - - Spring? , Apache CXF WS Spring bean beans...

. , .

0

, , setApplicationContext() applicationContextProvider. , .

Spring beans : bean . : bean ApplicationContextProviderTest, , , setApplicationContext() , .

, bean , . , Spring , :

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

Thus, the bean will be created when the application starts, and at this time the method will be called setApplicationContext(). Then you can use the afterword after the condition that the use occurs after the application starts.

Of course, a much better way is to automatically connect the bean to where you need it, but sometimes it is not possible (read: outdated applications).

0
source

Source: https://habr.com/ru/post/1795043/


All Articles