I already try deploying .WAR spring to tomcat 9 but have an error - failed to start component.
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);
}
}
AppConfig:
@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;
}
}
in web.xml, no servlet config. empty configuration. how to solve this problem? and when I put my url service which I already created, it's not found and I have a question. must I put the configuration when I deploying war in tomcat? even though, my code runs well before deploy. no problem.
Thanks. Bobby