문제

I have a Tomcat 7 webapp, in which I have two different ServletContextListener.

Example:

Listener A

public class ListenerA implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce){

        ObjectA objectA = new ObjectA();
        context.setAttribute("objectA", objectA);
    }

    ...

}

Listener B

public class ListenerB implements ServletContextListener {

    public void contextInitialized(ServletContextEvent sce){

        ObjectB objectB = new ObjectB();
        context.setAttribute("objectB", objectB);
    }

    ...

}

web.xml

...

<listener>
   <description>ListenerA</description>
   <listener-class>com.example.web</listener-class>
</listener>
<listener>
    <description>ListenerB</description>
    <listener-class>com.example.web</listener-class>
</listener>

...

¿Does the order of appearance in web.xml affect to load process, or are they loaded in different threads?

I'd like to know if it's possible to get ObjectA from context in ListenerB. I can always merge both listeners into a unique one so I'd be able to have both objects A and B in the same listener, but I want to know if this is possible.

I know I have the option to use attributeAdded from ServletContextAttributeEvent interface, but, will this be enough to do the work or can it fail if the ListenerB loads before the ListenerA?

도움이 되었습니까?

해결책

Ok, so I've finally found the answer here

All servlet containers an J2EE containers implement this part of the spec strictly. You can rely on the fact that the listeners are called in the order you specified in web.xml.

You can have a Application LEVEL Data structure(HashMap) that will be updated by each Filter/Listener as it encounters the data from the requests. This will let each Listener update only what is essential. You can put the common code in a base Listener so that there is no code duplication.

So the load order is determined strictly by the order of appearance in web.xml

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top