【发布时间】:2017-11-30 19:16:27
【问题描述】:
我已经尝试将 .WAR spring 部署到 tomcat 9 但出现错误 - 无法启动组件。
AppInitializer:
public class AppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(AppConfig.class);
ctx.setServletContext(container);
ServletRegistration.Dynamic servlet = container.addServlet(
"dispatcher", new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}
SpringBootWarDeploymentApplication:
@SpringBootApplication
public class SpringBootWarDeploymentApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringBootWarDeploymentApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(SpringBootWarDeploymentApplication.class, args);
}
}
应用配置:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.project.maven")
public class AppConfig extends WebMvcConfigurerAdapter {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
registry.addResourceHandler("**/**")
.addResourceLocations("classpath:/META-INF/resources/"); // harus ada folder resources di webapp/WEB-INF/
}
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
}
在 web.xml 中,没有 servlet 配置。空配置。如何解决这个问题呢?当我放置我已经创建的 url 服务时,找不到它,我有一个问题。在tomcat中部署war时必须放置配置吗?尽管如此,我的代码在部署之前运行良好。没问题。
谢谢。 鲍比
【问题讨论】:
标签: java spring spring-mvc tomcat war