大佬教程收集整理的这篇文章主要介绍了如何使用基于纯Java的配置来配置Spring MVC?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
你需要对进行以下更改,web.xml
以支持基于Java的配置。这将告诉你dispatcherServlet
使用基于注释的Java配置加载配置AnnotationConfigWebApplicationContext
。你只需要将Java配置文件的位置传递给contextConfigLOCATIOnparam
,如下所示
<servlet>
<servlet-name>springdispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.dispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLOCATIOn</param-name>
<param-value>/*path to your WebSpringConfig*/ </param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
更新:在不更改web.xml的情况下进行相同操作
你甚至可以在没有web.xml
Servlet规范3.0 web.xml
可选的情况下执行此操作。你只需要实现/配置WebApplicationInitializer
接口来配置ServletContext
,它将允许你以dispatcherServlet
编程方式创建,配置和执行注册。好处是可以WebApplicationInitializer
自动检测到。
总而言之,一个需要实现的WebApplicationInitializer
摆脱方法web.xml
。
public class MyWebAppInitializer implements WebApplicationInitializer {
@OverrIDe
public voID onStartup(ServletContext container) {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext =
new AnnotationConfigWebApplicationContext();
rootContext.register(WebSpringConfig.class);
// Manage the lifecycle of the root application context
container.addListener(new ContextLoaderListener(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("dispatcher", new dispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapPing("/");
}
}
我有一个我认为非常简单的Spring MVC设置。我的applicationContext.xml是这样的:
<mvc:Annotation-driven />
<mvc:resources mapping="/css/**" LOCATIOn="/css/" />
<context:property-placeholder LOCATIOn="classpath:controller-test.properties" />
<bean class="org.springframework.web.servlet.view.InternalresourceViewResolver"
p:prefix="/WEB-INF/views/" p:suffix=".jsp" />
我的web.xml当前是这样的:
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLOCATIOn</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
我正在尝试将此设置转换为基于Java的纯配置。我已经搜索了Web,到目前为止,我已经提出了一些东西(这些东西可以解释)(如何做),但是没有解释如何在环境(即Web上下文)中注册该Java配置。
到目前为止,我对@Configuration的了解是:
@Configuration
@EnableWebMvc
@Propertysource("classpath:controller.properties")
@ComponentScan("com.project.web")
public class WebSpringConfig extends WebMvcConfigurerAdapter {
@Override
public void addresourceHandlers(resourceHandlerRegistry registry) {
registry.addresourceHandler("/css/**").addresourceLOCATIOns("/css/");
}
@Bean
public ViewResolver configureViewResolver() {
InternalresourceViewResolver viewResolve = new InternalresourceViewResolver();
viewResolve.setPrefix("/WEB-INF/views/");
viewResolve.setSuffix(".jsp");
return viewResolve;
}
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
configurer.enable();
}
}
如何在Web容器中注册?我正在使用最新的Spring(4.02)。
谢谢!
@H_772_70@ @H_772_70@以上是大佬教程为你收集整理的如何使用基于纯Java的配置来配置Spring MVC?全部内容,希望文章能够帮你解决如何使用基于纯Java的配置来配置Spring MVC?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。