[英]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)?
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
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.