简体   繁体   English

`servlet-context.xml`,`root-context.xml`和`web.xml`的用途是什么?

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

I a new to Java Spring MVC web development. 我是Java Spring MVC Web开发的新手。 I am kind of confused by the 3 config files below. 我对下面的3个配置文件感到困惑。 They are auto created by the STS webmvc project template. 它们由STS webmvc项目模板自动创建。

  • What's the intended use of them? 它们的用途是什么?
  • Why do we need 3 config files rather than a single one? 为什么我们需要3个配置文件而不是一个?
  • Is there any special reason for their different locations? 他们不同的位置有什么特殊原因吗?

在此输入图像描述

root-context.xml is the Spring Root Application Context Configuration. root-context.xml是Spring Root Application Context Configuration。 It's optional. 这是可选的。 It's for configuring your non-web beans. 它用于配置非Web bean。 You need it for Spring Security or OpenEntityManagerInView Filter though. 但是你需要它用于Spring Security或OpenEntityManagerInView Filter。 It would be better to place it in meta-inf/spring . 将它放在meta-inf/spring会更好。

servlet-context.xml is the Spring Web Application Context Configuration. servlet-context.xml是Spring Web应用程序上下文配置。 It's for configuring your Spring beans in a web application. 它用于在Web应用程序中配置Spring bean。 If you use root-context.xml , you should put your non-web beans in root-context.xml , and web beans in servlet-context.xml . 如果使用root-context.xml ,则应将非Web bean放在root-context.xml ,将web bean放在servlet-context.xml

web.xml is for configuring your servlet container , such as Tomcat. web.xml用于配置servlet容器 ,例如Tomcat。 You need this one too. 你也需要这个。 It's for configuring servlet filters and the servlet. 它用于配置servlet过滤器和servlet。 web.xml is loaded first, then optionally loads your root context, then loads your web context. 首先加载web.xml ,然后可选地加载根上下文,然后加载Web上下文。

You can avoid using xml by using JavaConfig. 您可以通过使用JavaConfig避免使用xml。

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 创建文件名“javax.servlet.ServletContainerInitializer”(不带引号)文件内容将是实现此接口的类的完全限定名称,将文件放在/ META-INF / services

You may implement ServletContainerInitializer and override the method like this 您可以实现ServletContainerInitializer并覆盖此方法

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 在此之后,您不需要web.xml

Do remember if you are using maven to build your application mention this in pom.xml 请记住,如果您使用maven构建应用程序,请在pom.xml中提及此内容

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

Before that you have to write a configuration class using @Configuration and @Bean annotations 在此之前,您必须使用@Configuration和@Bean注释编写配置类

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 此配置类从servlet-context.xml替换<bean></bean>初始值设定项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM