Question

I have created a Spring MVC project through eclipse. I believe I used some plugins to generate the project directory. I find here there configuration files.

  1. web.xml
  2. root-context.xml
  3. servlet-context.xml

  4. I am kinda of familiar with Spring MVC & its dependency injection. However I have problems understanding the last two configuration files (root-context & servlet-context). What kind of configurations do they contain?

  5. Also in may online examples I see mvc-dispatcher-servlet.xml. Why did eclipse not generate this xml file in my project?

  6. [IMPORTANT] I wanted to set up strong security and user authentication for my web app. I have been following online tutorials again and they all create a seperate xml file named spring-security.xml and add the namespace information to that file. Does it suffice if I just create this file and add the name space information? I mean dont' I need to import this file to a main file that is scanned by Spring framework?

  7. How do I define and where do I put spring application context.xml file and start wiring the dependencies together? Also if I define everything (all dependencies here) how is this file picked up by the framework?

Thanks,

Was it helpful?

Solution

Configuration Files

If you check your web.xml you will find both of root-context.xml and servlet-context.xml files being referred here. One used by Dispatcher Servlet and other by Context Loader Listenter. You can name your files to whatever unless they are being refereed in web.xml

Eclipse Not generating files

Every editor works its own way. some may generate full fledged project/app with both DispatcherServlet and ContextLoaderListner configured or some with only DispatcherServlet ( with minimal configutaion). Check Spring Roo it starts with basic and gives you the flexibility to generate a strong app.

mvc-dispatcher-servlet.xml is not there

Some of the thing in spring projects are convention based, for example if you are not providing any file to your DispatcherServlet in web.xml spring looks for mvc-dispatcher-servlet.xml file, and if you have provided it won't look for.

Spring Security

To Configure Spring Security you need to provide at least some configuration. But the question is where. You need to add this configuration to your web.xml only. and Hence no need to import this to any other file.

 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-   class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/spring/spring-security.xml                          
    </param-value>
</context-param>
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Where to define application context.xml

Just define it any where, configure beans in it. You can add this file as follows:

a) Either Import this into some other configuration file like root-context.xml or servlet-context.xml as <import resource="application-context.xml"/>

b) Add this into web.xml with ContextLoaderListner as context param

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
            classpath*:META-INF/spring/application-context*.xml
            classpath*:META-INF/spring/abc*.xml
     </param-value>
</context-param>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top