从另一个WAR继承Spring配置

问题描述 投票:1回答:1

我们在Eclipse中有一个名为“Framework”的SpringMVC项目和一个必须从“Framework”配置继承的“New project”。

两个Spring项目都没有使用.xml配置文件。

“Framework”包含我们每次启动新项目时都不想重写的所有业务类,数据源配置和ContextListener默认行为。

“框架”在第二个项目中作为Maven叠加层加载。

正如您在下面看到的,它是SpringMVC配置的一个非常基本的示例。

骨架

-- 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, "/*");
    }
}

在第二个项目中,我成功导入了“Framework”应用程序上下文,但还没有设法导入WebApplicationContext,因为它导致NullPointerException。

新项目

-- 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, "/*");
    }
}

StackTrace显示:

Caused by: java.lang.NullPointerException
    at main.java.config.AppInit.onStartup(AppInit.java:50)
    at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:172)
java spring-mvc
1个回答
0
投票

您必须在当前配置中导入Config类:

import org.springframework.context.annotation.*;

@EnableAspectJAutoProxy
@EnableWebMvc
@ComponentScan(basePackages="main.java")
@Import({WebConfiguration.class, ApplicationContextConfiguration.class })
public class AppConfig implements WebMvcConfigurer {
//Your current configuration
}
© www.soinside.com 2019 - 2024. All rights reserved.