gpt4 book ai didi

java - 从另一个 WAR 继承 Spring 配置

转载 作者:太空宇宙 更新时间:2023-11-04 10:13:02 25 4
gpt4 key购买 nike

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

两个 Spring 项目均未使用 .xml 配置文件。

“框架”包含所有业务类、数据源配置和 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)

最佳答案

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

import org.springframework.context.annotation.*;

@EnableAspectJAutoProxy
@EnableWebMvc
@ComponentScan(basePackages="main.java")
@Import({WebConfiguration.class, ApplicationContextConfiguration.class })
public class AppConfig implements WebMvcConfigurer {
//Your current configuration
}

关于java - 从另一个 WAR 继承 Spring 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52091922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com