如何使用纯Java配置Spring MVC?
我有,我认为一个非常简单的Spring MVC设置。我的应用程序上下文.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" />
我的网络.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,到目前为止,我已经想出了解释(一些什么)如何执行Java配置的东西,但没有解释如何将Java配置注册到环境中,即Web上下文。
到目前为止,我在@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 容器?我正在使用最新的弹簧(4.02)。
谢谢!