简体   繁体   English

在Spring启动应用上加载不同的应用程序上下文

[英]load differents application context on spring boot apps

Im trying to load differents context on differents applications, let me explain: 我试图在不同的应用程序上加载不同的上下文,让我解释一下:

I have 3 apps (DAOS - Helpers - Web) all of them are SpringBoot Apps but Web is WebApp (which has ServletInitializer) 我有3个应用程序(DAOS - Helpers - Web),所有这些应用程序都是SpringBoot应用程序,但Web是WebApp(具有ServletInitializer)

The things is I tested each one with JUnit and their applicationContext its ok for each one, but when I deploy Web Project (.war) I got this error 事情是我用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]

I Already tried @Import(Class.class) but no good results. 我已经尝试了@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/");
    }



}

all contexts are in /src/main/resources and in JUnit as I said they are fine, my JUnits are ok. 所有的上下文都在/ src / main / resources和JUnit中,因为我说它们没问题,我的JUnit还可以。

So the question is, how can I load the contexts (Helper - DAO) on "main" application? 所以问题是,如何在“主”应用程序上加载上下文(Helper-DAO)?

UPDATE 1 更新1

after add classpath:/context.xml i got this error: 添加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. 
UPDATE 2 更新2

I added "spring.main.allow-bean-definition-overriding=true" to my properties on Web project and its works, but I'd like to know why is overriding if defined context just once. 我在Web项目及其工作中添加了“spring.main.allow-bean-definition-overriding = true”到我的属性,但我想知道为什么如果只定义了一次上下文则覆盖。

If they are packaged part of other JARs on the classpath then use the following 如果它们在classpath中打包为其他JAR的一部分,则使用以下命令

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

This would look for the resource within the JARs on the classpath . 这将在classpath上查找JAR中的资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在Spring Boot应用程序中加载应用程序上下文 - Not able to load Application Context in Spring Boot app 使用 Camel 的 Spring Boot 无法加载应用程序上下文 - Spring Boot with Camel failed to load application context Spring boot 中的父应用程序上下文 - Parent Application Context in Spring boot 如何加载,在 Spring 引导测试中,基本 Spring 应用程序上下文仅添加命名组件? - How to Load, in Spring Boot Test, Basic Spring Application-Context Adding Only Named Components? Spring Application Context Load Order - Spring Application Context Load Order 如何加载Spring Application Context - How to load Spring Application Context 无法在 Spring Boot 测试中加载上下文属性 - Cannot load context properties in a Spring Boot test 如何在运行时使用 Cucumber 而不是通过测试加载 Spring Boot 应用程序上下文? - How to load Spring Boot Application Context while using Cucumber at runtime, not via tests? 在 JUnit 测试类中使用属性值而不加载整个 Spring 引导应用程序上下文 - Using property values in JUnit test classes without load whole Spring Boot application context Spring Boot:在类路径更改中刷新应用程序上下文 - Spring Boot: Refresh Application Context on Classpath Changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM