gpt4 book ai didi

java - 在 Spring Boot 应用程序中加载不同的应用程序上下文

转载 作者:行者123 更新时间:2023-12-02 05:48:47 25 4
gpt4 key购买 nike

我正在尝试在不同的应用程序上加载不同的上下文,让我解释一下:

我有 3 个应用程序(DAOS - Helpers - Web),它们都是 SpringBoot 应用程序,但 Web 是 WebApp(具有 ServletInitializer)

问题是,我使用 JUnit 及其 applicationContext 测试了每个项目,每个项目都没有问题,但是当我部署 Web 项目 (.war) 时,我收到此错误

    org.springframework.beans.factory.BeanDefinitionStoreException: 
IOException parsing XML document from ServletContext resource
[/helper-context.xml]; nested exception is
java.io.FileNotFoundException: Could not open ServletContext resource
[/helper-context.xml]

我已经尝试过 @Import(Class.class) 但没有好的结果。

@SpringBootApplication()
@ImportResource("/app-context.xml")
public class BaseApplication {

public static void main(String[] args) {
SpringApplication.run(BaseApplication.class, args);
}
}
@SpringBootApplication()
@ImportResource("/helper-context.xml")
@Import(BaseApplication.class)
public class HelperApplication {

public static void main(String[] args) {
SpringApplication.run(HelperApplication.class, args);
}
}
@Configuration
@Import(HelperApplication.class)
public class ServletInitializer extends SpringBootServletInitializer implements WebApplicationInitializer, WebMvcConfigurer{

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(PortalProveedoresRegionalApplication.class);
}

@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}


@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/resources/");
}



}

所有上下文都在/src/main/resources 和 JUnit 中,正如我所说,它们很好,我的 JUnit 也很好。

所以问题是,如何在“主”应用程序上加载上下文(Helper - DAO)?

更新1

添加 classpath:/context.xml 后出现此错误:

Caused by: org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to register bean definition with name 'documentoDaoHelper'
Offending resource: class path resource [helper-context.xml]; nested exception is org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'documentoDaoHelper' defined in class path resource [helper-context.xml]: Cannot register bean definition [Generic bean: class [com.sovos.helper.helpers.DocumentoDaoHelper]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [helper-context.xml]] for bean 'documentoDaoHelper': There is already [Generic bean: class [com.sovos.helper.helpers.DocumentoDaoHelper]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in URL [jar:file:/home/asp4demos/portalregional/tomcat-8.5.28/webapps/PortalProveedores/WEB-INF/lib/helper-0.0.1.jar!/com/sovos/helper/helpers/DocumentoDaoHelper.class]] bound.
更新2

我在 Web 项目及其作品的属性中添加了“spring.main.allow-bean-definition-overriding=true”,但我想知道如果定义的上下文仅一次,为什么会覆盖。

最佳答案

如果它们是 classpath 上其他 JAR 的打包部分,则使用以下内容

@ImportResource("classpath:/app-context.xml")

这将在类路径上的 JAR 中查找资源。

关于java - 在 Spring Boot 应用程序中加载不同的应用程序上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56069993/

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