0
votes

When the application is started two contexts are being raised: one for org.springframework.web.context.WebApplicationContext event and the other for org.springframework.web.context.WebApplicationContext:/dispatcher event and everything works fine, dispatcher context uses "parent" beans instead of creating its own. When I initiate spring context refresh using ConfigurableApplicationContext::refresh() (I am calling it using rest api request) only one ContextRefreshEvent is being sent (the first one) and when I try to access any servlet-components (rest controllers), dispatcher starts creating his own beans though there are "main" beans created after refresh. And once again no events are being sent for dispatcher context refresh. I need to make dispatcher context to use beans from refreshed web application context instead of creating its own. I think the problem is because dispatcher context is not being refreshed with the main context. Maybe it doesn't even know anything about refreshed context has been raised.

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/application-*.xml</param-value>
    </context-param>

    <listener>
        <listener-class>com.app.web.CustomContextListener</listener-class>
    </listener>

    <listener>
        <listener-class>
            org.springframework.security.web.session.HttpSessionEventPublisher
        </listener-class>
    </listener>

    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/config.json</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.js</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.map</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.eot</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.css</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.woff</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.woff2</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>/assets/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.svg</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.ttf</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.ico</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.png</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.gif</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpg</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.jpeg</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.bmp</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>default</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>

application-context.xml

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

    <context:component-scan base-package="com.app"/>
    <import resource="classpath*:configuration-context.xml"/>

    <mvc:annotation-driven/>
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <bean class="com.ais.configuration.collection.CustomConfigLoader">
        <constructor-arg name="pathsHolder" ref="applicationPaths"/>
        <property name="location" value="app.properties"/>
        <property name="searchSystemEnvironment" value="true"/>
        <property name="ignoreResourceNotFound" value="false"/>
        <property name="ignoreUnresolvablePlaceholders" value="false"/>
    </bean>
</beans>

dispatcher-servlet.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">
</beans>
1
You shouldn't be refreshing the beans in the first place.M. Deinum
@M.Deinum well I know, but we all have to do something we shouldn't do and we don't want to do sometimes. And this is not my personal project or something I have full control at. The app still works fine even with this problem and if it is unsolvable I will just leave it as it isharald

1 Answers

0
votes

For anyone else looking for answer on this question this is how I solved the problem

I created utility class to store all ApplicationContexts in app

public class ApplicationContextUtils
{
    public static final Set<ApplicationContext> AppContexts = new LinkedHashSet<>();
}

Then I wrote class to handle all ContextRefreshed events and put corresponding contexts in set in utility class

@Component
public class ContextRefreshedEventsHandler implements ApplicationListener<ContextRefreshedEvent>
{
    @Override
    public void onApplicationEvent( ContextRefreshedEvent event )
    {
        ApplicationContext app = event.getApplicationContext();
        ApplicationContextUtils.AppContexts.add( app );
    }
}

So when the app starts all the contexts are being stored in my utility class in order they appear. And in my RefreshService I iterate through all the contexes and refresh them

List<ApplicationContext> appContexts = new ArrayList<>( ApplicationContextUtils.AppContexts );

        for( ApplicationContext applicationContext : appContexts )
            ( (ConfigurableApplicationContext)applicationContext ).refresh();

this solves the problem