在Spring Boot应用程序上加载差异应用程序上下文

时间:2019-05-10 02:41:24

标签: java spring spring-boot

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

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

问题是我用JUnit及其applicationContext测试了每个组件,每个组件都可以,但是当我部署Web Project(.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”,但我想知道为什么如果只定义一次上下文就覆盖。

1 个答案:

答案 0 :(得分:0)

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

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

这将在classpath上的JAR中查找资源。