sách gpt4 ăn đã đi

java - 根上下文和调度程序 servlet 上下文到底是如何进入 Spring MVC Web 应用程序的?

In lại Tác giả: IT Lão Cao 更新时间:2023-10-28 13:50:03 27 4
mua khóa gpt4 giày nike

我正在学习Spring MVC,我有一些疑问

所以,我有这个配置类来配置处理用户请求的 DispatcherServlet:

public class MyWebAppInitializer implements WebApplicationInitializer {

@Ghi đè
public void onStartup(ServletContext container) {

// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = ...
// Create the dispatcher servlet's Spring application context
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();

dispatcherContext.register(DispatcherConfig.class);

// Register and map the dispatcher servlet
ServletRegistration.Dynamic dispatcher = container.addServlet("main", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("main/");
}
}

DispatcherServlet 的工作原理对我来说非常清楚。我的疑问与 bối cảnh 概念有关。

1) 上下文究竟代表什么?我认为这就像一组具有特定用途的 bean ,并且可以在环境中协同工作。但我对这个断言绝对不正确。

2) root contextdispatcher servlet context 有什么区别?

3) 据我了解,dispatcherContext 中定义的 bean 可以访问 rootContext 中定义的 bean(但反之则不然)。为什么?

Tnx

câu trả lời hay nhất

根上下文

Spring 应用程序中的根上下文是 ApplicationContextphụ thuộc vào ContextLoaderListener 加载.此上下文应具有全局可用的资源,如服务、存储库、基础设施 bean(DataSourceEntityManagerFactory 等)等。

ContextLoaderListenerhiện hữu ServletContext 中注册此上下文名下org.springframework.web.context.WebApplicationContext.ROOT .

如果您加载 ApplicationContext并在 ServletContext 中使用上面的名称注册它然后将有资格作为根上下文。

子上下文

Spring 应用程序中的子上下文是 ApplicationContextphụ thuộc vào DispatcherServlet 加载(或者例如 Spring-WS 应用程序中的 MessageDispatcherServlet)。此上下文应仅包含与该上下文相关的 bean,对于 Spring MVC,它将是 ViewResolver年代,HandlerMappingvân vân.

servlet 在 ServletContext 中注册此上下文。名下org.springframework.web.servlet.FrameworkServlet.CONTEXT. .

根<-子关系

只有子上下文可以访问父上下文,因为您可以有多个子上下文。例如在 Spring MVC 与 Spring WS 应用程序的结合中。子上下文通过在 ServletContext 中查找来检测父上下文。用众所周知的名字。

如果根上下文可以访问子上下文,它将使用哪一个来连接 bean?除此之外,如果是这样的话,当涉及 AOP 时,您也会得到令人惊讶的结果。在子上下文中定义的 AOP 会突然影响在根上下文中配置的 bean。

关于java - 根上下文和调度程序 servlet 上下文到底是如何进入 Spring MVC Web 应用程序的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30639842/

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress