We have a SpringMVC project called "Framework" in Eclipse and a "New project" that must inherit from the "Framework" configuration.
Both Spring projects are not using .xml configuration files.
The "Framework" contains all business classes, datasource configuration and ContextListener default behavior that we don't want to rewrite each time we start a new project.
The "Framework" is loaded as a Maven overlay in the second project.
As you can see below, it is a very basic sample SpringMVC configuration.
Framework
-- main
-- java
-- config
- AppConfig.class (implements WebMvcConfigurer)
- AppInit.class (implements WebApplicationInitializer)
AppConfig.class
@EnableAspectJAutoProxy
@EnableWebMvc
@ComponentScan(basePackages="main.java")
public class AppConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new ControllerLoggingInterceptor()).addPathPatterns("/*");
}
@Bean
public ViewResolver getViewResolver(){
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
registry.addResourceHandler("/images/**").addResourceLocations("/resources/images");
}
@Bean
public ReloadableResourceBundleMessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasenames("classpath:messages","classpath:/framework/messages");
return messageSource;
}
@Bean
public javax.validation.Validator localValidatorFactoryBean() {
return new LocalValidatorFactoryBean();
}
@Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(838860800);
return multipartResolver;
}
}
AppInit.class
public class AppInit implements WebApplicationInitializer {
@Autowired
MapperInfosDB mapperUserBD;
@Override
public void onStartup(ServletContext container) {
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(AppConfig.class);
container.addListener(new ContextLoaderListener(appContext));
AnnotationConfigWebApplicationContext dispatcherContext =
new AnnotationConfigWebApplicationContext();
dispatcherContext.register(AppConfig.class);
ServletRegistration.Dynamic dispatcher =
container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.addMapping("/");
FilterRegistration.Dynamic fr = container.addFilter("encodingFilter", new CharacterEncodingFilter());
fr.setInitParameter("encoding", "UTF-8");
fr.setInitParameter("forceEncoding", "true");
fr.addMappingForUrlPatterns(null, false, "/*");
fr = container.addFilter("RequestLoggingFilter", new RequestLoggingFilter());
fr.addMappingForUrlPatterns(null, true, "/*");
}
}
In the second project, I import the "Framework" application context successfully, but yet haven't managed to import the WebApplicationContext as it leads to a NullPointerException.
New project
-- main
-- java
-- config
- ApplicationContextConfiguration.class (extends ContextLoaderListener implements WebMvcConfigurer)
- WebConfiguration.class (implements WebApplicationInitializer)
ApplicationContextConfiguration.class
@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy
public class ApplicationContextConfiguration extends ContextLoaderListener implements WebMvcConfigurer {
@Override
protected ApplicationContext loadParentContext(ServletContext servletContext) {
ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
// Do some stuff on applicationContext inherited from Framework
return ac;
}
}
WebConfiguration.class
@Configuration
public class WebConfiguration implements WebApplicationInitializer {
private static final String[] BASE_PACKAGE = { "main.java", "new.project.packages" };
@Override
public void onStartup(ServletContext container) {
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.scan(BASE_PACKAGE);
dispatcherContext.register(AppConfig.class);
dispatcherContext.register(AppInit.class);
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.addMapping("/");
dispatcher.setLoadOnStartup(1);
FilterRegistration.Dynamic fr = container.addFilter("encodingFilter", new CharacterEncodingFilter());
fr.setInitParameter("encoding", "UTF-8");
fr.setInitParameter("forceEncoding", "true");
fr.addMappingForUrlPatterns(null, false, "/*");
fr.addMappingForUrlPatterns(null, true, "/*");
}
}
The StackTrace shows :
Caused by: java.lang.NullPointerException
at main.java.config.AppInit.onStartup(AppInit.java:50)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:172)