applicationContext.xml和spring-servlet.xml在Spring框架无论如何有关系吗?applicationContext.xml是否可用DispatcherServlet?*-servlet.xml?为什么applicationContext.xml单独不足?应用程序上下文和Web应用程序上下文有什么区别?
我知道WebApplicationContext用于面向Spring MVC架构的应用程序?
我想知道ApplicationContextMVC应用程序有什么用?什么样的豆类定义ApplicationContext?
我正在学习Spring MVC,我有一些疑问
所以,我有这个配置类来配置处理用户请求的DispatcherServlet:
public class MyWebAppInitializer implements WebApplicationInitializer {
@Override
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/");
}
}
Run Code Online (Sandbox Code Playgroud)
我很清楚DispatcherServlet是如何工作的.我的怀疑与背景概念有关.
1)究竟代表什么背景?我认为这类似于一组具有特定pourpose并且可以在环境中工作的bean.但我绝对不是这个断言的真实.
2)根上下文和调度程序servlet上下文之间有什么区别?
3)根据我的理解,dispatcherContext中定义的bean 可以访问rootContext中定义的 …
我知道我需要注册@Controller在我的servlet上下文中注释的类,以使我的webapp可访问.通常,我按照以下方式进行:
@Configuration
@EnableWebMvc
@ComponentScan({"foo.bar.controller"})
public class WebConfig extends WebMvcConfigurerAdapter {
//other stuff like ViewResolvers, MessageResolvers, MessageConverters, etc.
}
Run Code Online (Sandbox Code Playgroud)
我添加到根应用程序上下文中的所有其他配置类.以下是我的dispetcher初始化程序通常如下所示:
public class DispatcherServletInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[] { RootConfig.class, ServiceConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[] { WebConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我开始使用WebSockets时,事情变得越来越有趣.要使websockets正常工作,您必须将WebSoketConfig.class放入servlet上下文.这是我的WebSocketConfig示例:
@Configuration
@EnableScheduling
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) …Run Code Online (Sandbox Code Playgroud) 我对这两个概念感到困惑.阅读Spring文档,我发现,例如.bean工厂是Spring容器.我还读到"ApplicationContext是BeanFactory的完整超集".但两者之间的差异并不明显.那么区别是什么呢?