我试图在不同的应用程序上加载不同的上下文,让我解释一下:
我有3个应用程序(DAOS-帮助程序-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”,但我想知道如果定义的上下文仅覆盖一次,为什么会覆盖。
参考方案
如果它们是classpath
上其他JAR的一部分打包,则使用以下命令
@ImportResource("classpath:/app-context.xml")
这将在classpath
上的JAR中查找资源。
我正在尝试将SAML OKTA集成到春季启动应用程序中。我需要在Spring Boot中使用以下bean设置:谁能帮我在Spring Boot的webSSOprofileConsumer bean中设置responseSkew属性。我只需要上面在基于注解的Spring Boot注入中提到的xml配置的等效Spring注入技术。我已经通过链接了:http://…
在Spring Kafka中,我需要在应用程序中添加@EnableKafka批注吗? - java我看到有人在他们的Spring Boot应用程序中添加@EnableKafka,我想知道为什么。我有一个工作的春季靴kafka生产者和使用者,并且没有使用@EnableKafka。那么,为什么人们需要显式添加它?谢谢。 java大神给出的解决方案 这是因为Spring Boot通过KafkaAutoConfiguration类(javadoc)为Kafka提…
java:继承 - java有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…
IntelliJ Spring MVC教程部署 - java我尝试了tutorial,当我尝试部署webapp(IntelliJ 13.1.4 Ultimate)时,出现了一个奇怪的错误,如下面的屏幕快照所示。解决此错误的方法是什么? org.jdom.input.JDOMParseException: Error on line 742: The content of elements must consist o…
Java-如何将此字符串转换为日期? - java我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…