What's the intended use of `servlet-context.xml`, `root-context.xml` and `web.xml`?

人走茶凉 提交于 2021-02-06 11:47:18

问题


I a new to Java Spring MVC web development. I am kind of confused by the 3 config files below. They are auto created by the STS webmvc project template.

  • What's the intended use of them?
  • Why do we need 3 config files rather than a single one?
  • Is there any special reason for their different locations?

enter image description here


回答1:


root-context.xml is the Spring Root Application Context Configuration. It's optional. It's for configuring your non-web beans. You need it for Spring Security or OpenEntityManagerInView Filter though. It would be better to place it in meta-inf/spring.

servlet-context.xml is the Spring Web Application Context Configuration. It's for configuring your Spring beans in a web application. If you use root-context.xml, you should put your non-web beans in root-context.xml, and web beans in servlet-context.xml.

web.xml is for configuring your servlet container, such as Tomcat. You need this one too. It's for configuring servlet filters and the servlet. web.xml is loaded first, then optionally loads your root context, then loads your web context.

You can avoid using xml by using JavaConfig.




回答2:


Create a file name "javax.servlet.ServletContainerInitializer" (without quotes) the file content will be fully qualified name of the class implementing this interface, put the file here /META-INF/services

You may implement ServletContainerInitializer and override the method like this

public class CourtServletContainerInitializer implements ServletContainerInitializer {

    @Override
    public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
        AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
        applicationContext.register(CourtConfiguration.class);

        DispatcherServlet dispatcherServlet = new DispatcherServlet(applicationContext);

        ServletRegistration.Dynamic registration = ctx.addServlet("court", dispatcherServlet);
        registration.setLoadOnStartup(1);
        registration.addMapping("/");

    }

}

After this you do not need web.xml

Do remember if you are using maven to build your application mention this in pom.xml

<properties>
        <failOnMissingWebXml>false</failOnMissingWebXml>
</properties>

Before that you have to write a configuration class using @Configuration and @Bean annotations

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@ComponentScan(basePackages = "com.practice.learnspringmvc.*")

public class CourtConfiguration {

    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver internalResourceViewResolver = new InternalResourceViewResolver();
        internalResourceViewResolver.setPrefix("/WEB-INF/views/");
        internalResourceViewResolver.setSuffix(".jsp");
        return internalResourceViewResolver;
    }
}

This configuration class replaces your <bean></bean> initializers from servlet-context.xml



来源:https://stackoverflow.com/questions/27893886/whats-the-intended-use-of-servlet-context-xml-root-context-xml-and-web-xm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!