当前位置: 首页 > 知识库问答 >
问题:

遗留代码中的Spring Boot集成

常波鸿
2023-03-14

我很难将spring-boot应用程序作为另一个java(非Spring)应用程序的依赖项。

我被要求用一些方法和对Oracle数据库的访问来开发一个新的REST API。为此,我使用了Spring-boot,应用程序本身或部署在服务器上都运行良好。经过几次调整,我把它分成了几个模块,如下所示:

  • models:包含业务代码的模型对象
  • API:包含业务代码和实际API
    • 对模型有依赖关系
    • 包含连接到数据库所需的所有属性
      null
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan  base-package="be.belga.mediaservice"/>
        <context:annotation-config />
    </beans>
    
    @Component
    @EnableJpaRepositories("be.belga.mediaservice.dao")
    public class SpringContext {
    
    private SpringContext() {}
    
    
    private static class ApplicationContextHolder {
        private static ApplicationContext instance = new ClassPathXmlApplicationContext("application-context.xml");
    }
    
    public static ApplicationContext getInstance() {
        return ApplicationContextHolder.instance;
    }
    }
    
    MediaServiceAPI mediaServiceAPI = (MediaServiceAPI) 
    SpringContext.getInstance().getBean("mediaServiceAPI");
    
    Exception in thread "main" java.lang.ExceptionInInitializerError
    at be.belga.mediaservice.util.SpringContext.getInstance(SpringContext.java:20)
    at Test.main(Test.java:10)
    Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mediaValueServiceImpl' defined in URL [jar:file:/home/mike/.m2/repository/be/belga/mediaservice-api/0.1.7/mediaservice-api-0.1.7.jar!/be/belga/mediaservice/service/impl/MediaValueServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'socialMediaServiceImpl' defined in URL [jar:file:/home/mike/.m2/repository/be/belga/mediaservice-api/0.1.7/mediaservice-api-0.1.7.jar!/be/belga/mediaservice/service/impl/SocialMediaServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'socialMetricsRepository': Cannot create inner bean '(inner bean)#5afa3c9' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#5afa3c9': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    13:39:04.731 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Retrieved dependent beans for bean '(inner bean)#7ef82753': [socialMetricsRepository]
    13:39:04.731 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Retrieved dependent beans for bean '(inner bean)#202b0582': [(inner bean)#7ef82753]
    13:39:04.732 [main] WARN org.springframework.context.support.ClassPathXmlApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mediaValueServiceImpl' defined in URL [jar:file:/home/mike/.m2/repository/be/belga/mediaservice-api/0.1.7/mediaservice-api-0.1.7.jar!/be/belga/mediaservice/service/impl/MediaValueServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'socialMediaServiceImpl' defined in URL [jar:file:/home/mike/.m2/repository/be/belga/mediaservice-api/0.1.7/mediaservice-api-0.1.7.jar!/be/belga/mediaservice/service/impl/SocialMediaServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'socialMetricsRepository': Cannot create inner bean '(inner bean)#5afa3c9' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#5afa3c9': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
    13:39:04.732 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1283bb96: defining beans [mediaValueForPressCommand,mediaValueServiceImpl,socialMediaServiceImpl,mediaReachScoreServiceImpl,XMLUtil,springContext,mediaServiceAPI,twitterSocialMediaServiceFacadeImpl,youtubeRestSocialMediaServiceFacadeImpl,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,org.springframework.data.jpa.repository.config.JpaRepositoryConfigExtension#0,emBeanDefinitionRegistrarPostProcessor,jpaMappingContext,jpaContext,agenciesMetricsRepository,CIMMetricsRepository,systemMetricsRepository,socialMetricsRepository,serviceTypeValueRepository,keywordsRepository]; root of factory hierarchy
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at be.belga.mediaservice.util.SpringContext$ApplicationContextHolder.<clinit>(SpringContext.java:16)
    ... 2 more
    Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'socialMediaServiceImpl' defined in URL [jar:file:/home/mike/.m2/repository/be/belga/mediaservice-api/0.1.7/mediaservice-api-0.1.7.jar!/be/belga/mediaservice/service/impl/SocialMediaServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'socialMetricsRepository': Cannot create inner bean '(inner bean)#5afa3c9' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#5afa3c9': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1193)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    ... 17 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'socialMetricsRepository': Cannot create inner bean '(inner bean)#5afa3c9' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#5afa3c9': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:313)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:129)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1531)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1276)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
    ... 31 more
    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#5afa3c9': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
    at org.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:634)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:448)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1173)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1067)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:299)
    ... 45 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
    ... 53 more
    

    是否有一种方法可以实际使用我的应用程序的API部分作为其他java应用程序的依赖项?

共有1个答案

翟英达
2023-03-14

您的核心API、模型和实现不应该依赖于Spring容器,在这种情况下,任何其他Java应用程序都可以自由地使用它们,只要它能够提供所有所需的依赖关系。

你可能会想,“好吧,但这对我有什么帮助?”。在想出一个有效的解决方案之前,我们需要了解更多。

当你说你已经把事情分成模块时,你是说一个单独的罐子来存放模型、API和REST的东西?

这些JAR应该可以作为其他应用程序的依赖项直接使用。其他应用程序应该不需要使用Spring(除非它们已经在使用Spring)。

 类似资料:
  • 尽管维基百科上对遗留系统的定义是: 一种旧的方法、旧的技术、旧的计算机系统或应用程序。 但是实际上,当你看到某个网站宣称用新的框架来替换旧的框架的时候,你应该知晓他们原有的系统是遗留系统。人们已经不想在上面工作了,很多代码也不知道是干什么的,也没有人想去深究——毕竟不是自己的代码。判断是否是遗留代码的条件很简单,维护成本是否比开发成本高很多。 几乎无法维护 代码遗失 逻辑不清 没有文档或者不够详细

  • 我正在处理一些遗留代码,其中所有Spring DAO都用@Transactional注释注释。现在我有一个业务需求,我需要在我的服务层中调用两个不同的DAO,如果事务在任何时候失败,就回滚它。 如何在Spring5中实现这一点,而不从DAO中删除@Transactional注释,并且仍然从服务层使用它们。我认为下面的代码不起作用,因为每个DAO中的事务彼此独立。 非常感谢提前。

  • 本文向大家介绍springboot集成activemq的实例代码,包括了springboot集成activemq的实例代码的使用技巧和注意事项,需要的朋友参考一下 ActiveMQ ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久的事情了,但是JM

  • 虽然Django最适合用来开发新的应用,但也可以将它整合到遗留的数据库中。Django包含了很多工具,尽可能自动化解决这类问题。 这篇文章假设你了解Django的基础部分,它们在教程中提及。 一旦你的Django环境建立好之后,你可以按照这个大致的流程,整合你的现有数据库。 向Django提供你的数据库参数 你需要告诉Django你的数据库连接参数,以及数据库的名称。请修改DATABASES设置,

  • 问题内容: 我最近听说过,然后我在一个网站上看到 传统类用于在Collections出现之前保存对象。 那么,为什么这些不被称为类,为什么呢? 问题答案: 传统类和接口是在Java 早期版本 中形成集合框架的类和接口,以及现在如何对其进行重组或重新设计。它们与框架完全兼容。 正式 不被弃用。 重新设计了所有旧类,以支持JDK5中的泛型。 Pratically 都不会被弃用,但也有其他类比较合适。

  • 本文向大家介绍springboot集成spring cache缓存示例代码,包括了springboot集成spring cache缓存示例代码的使用技巧和注意事项,需要的朋友参考一下 本文介绍如何在springboot中使用默认的spring cache, 声明式缓存 Spring 定义 CacheManager 和 Cache 接口用来统一不同的缓存技术。例如 JCache、 EhCache、