This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
Why do i have duplicate entries in the context-param and init param for the dispatcher servlet.I am not having the xxx-servlet file which the dispatcher servlet searches by default.I have all the bean definitions in the two xml files given.So,why the duplicate entries?
I am going to answer both threads you have in this one post.
ContextLoaderListener is a ServletListener. So in the JSP/Servlet spec a Servlet Listener gets called by the container when certain events occur. In this case it gets called right after when the ServletContext is created for the web application. When it gets called, inside the ContextLoaderListener is code to create/instantiate an ApplicationContext and load in the xml file you set with the context-param name and value tags. In those xml files you define your middle tier beans only. No Web Tier beans should be in the xml file/files that is loaded by the ContextLoaderListener.
The DispatcherServlet in its init method also creates an ApplicationContext, but this one should load in only the Web Tier beans like your Controllers, ViewResolvers, MappingHandlers etc.
So the reason why you do this is to separate the web tier from the middle tier. The web tier beans can see the middle tier beans, but not the other way around.
In your example you got it wrong because you put the same xml config files to be loaded by both the ContextLoaderListener and the DispatcherServlet.
So my guess is that your /WEB-INF/mvc-config.xml should be in the DispatcherServlet config and the
repository-config.xml is what should be in the <context-param> with your ContextLoaderListener.
Thanks Mark for the reply.
It really helped.
I want to have one more doubt get cleared.In the last xml file i posted,do I have two ApplicationContexts genearted then?One by the DispatcherServlet and other by ContextLoaderListener?Also,do i have all my beans created when my application context gets created/loaded?
Who is called first,DispatcherServlet or ContextLoaderListener?
All the beans you have configured in your xml have been created twice. You duplicated the xml files in both the ContextLoaderListener and DispatcherServlet web.xml configuration, but one instance of each bean in each application context. You get two ApplicationContexts created.
In JSP/Servlets, what gets created first the ServletContext or a Servlet?
That answers which application context gets created first. DispatcherServlet is a Servlet. ContextLoaderListener gets called right after the ServletContext gets created.