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

在同一JVM上运行多个Spring boot应用程序

红存
2023-03-14

我有一个多Maven模块Spring Boot项目,具有以下结构:

parent
   |_ pom.xml
   |_ webservices
        |_ src/main/java
             |_ webservices
                  |_ WebServicesConfig.java
                  |_ WebServicesStarter.java
                  |_ GlobalPropertiesLoader.java
        |_ pom.xml
   |_ backend
        |_ src/main/java
             |_ backend
                  |_ BackendStarter.java
        |_ pom.xml
   |_ commons
        |_ src/main/java
             |_ commons
                  |_ GlobalPropertiesDAO.java
                  |_ GlobalPropertiesRepository.java
                  |_ CommonsConfig.java;
        |_ pom.xml

webservices和backend都是单独的Spring Boot应用程序(它们生成一个jar文件,我用它来启动它们),它们依赖于commons模块。因此,我将commons作为一个依赖项包含在webservices和后端的pom中。xml。

我对启动我的应用程序没有什么疑问。

  1. 如何在单个JVM中启动后端和webservices?(在同一端口上)
  2. 我想在我的后端和webservices项目中自动连接GlobalPropertiesRepository(位于commons项目中)。我该怎么做?我可以自动连接不同模块吗?仅仅导入commons是行不通的。它抛出一个“无bean定义错误”。我认为这是因为如果导入GlobalPropertiesRepository,它不是由Spring容器启动的

=============更新=============

为应用程序html" target="_blank">添加我的配置类:

Commons应用程序现在有一个空的Configuration类,因为我只有我的Repository类在那里。下面是空的配置类:

package commons;

import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonsConfig {

}

这是GlobalProperty仓库:

package commons;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface GlobalPropertiesRepository extends CrudRepository<GlobalPropertiesDAO, Long>{
}

以下是webservices应用程序中必需的类:

入门类:

package webservices;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;

@SpringBootApplication
@EnableJpaRepositories
@ComponentScan({"commons", "webservices"})
public class WebServicesStarter {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(WebServicesStarter.class, args);

        ClassPathScanningCandidateComponentProvider provider =
                new ClassPathScanningCandidateComponentProvider(true);
    }

}

配置类:

package webservices;


import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import commons.CommonsConfig;

@Configuration
@Import(CommonsConfig.class)
public class WebServicesConfig {

    @Autowired CommonsConfig commonsConfig;
    public WebServicesConfig() {
    }
}

以及我试图自动连接存储库的类:

package webservices;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import commons.GlobalPropertiesDAO;
import commons.GlobalPropertiesRepository;

@Component
@Scope("singleton")
public class GlobalPropertiesLoader {

    @Autowired
    public GlobalPropertiesRepository globalPropertiesRepository;

    private GlobalPropertiesDAO globalProperties;

    @PostConstruct
    public void init(){
        globalProperties = globalPropertiesRepository.findOne(1L);
    }

    public GlobalPropertiesDAO getGlobalProperties(){
        return globalProperties;
    }

}

这是我得到的错误:

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public commons.GlobalPropertiesRepository webservices.GlobalPropertiesLoader.globalPropertiesRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [commons.GlobalPropertiesRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)

谢谢

共有1个答案

解高昂
2023-03-14

我想我知道是什么导致了这个错误。

在您的应用程序启动器类中,即使您有@ComponentScan({"Commons","webservice"})出于某种原因,@Repositorybean被忽略了。我认为问题出在类ClassPathBean定义扫描。根据留档JavaDoc,它确实声明默认情况下它将扫描所有组件,包括@Repository。但是当它识别存储库接口时,它会在方法isCandidateComponent中再次检查失败。(如果您查看源代码)

所以当您启用调试时,我们会得到这个日志

2015-05-18 11:10:13.399调试67917---[主]o.s.c.a.ClassPathBeanDefinitionScanner:忽略,因为不是具体的顶级类:文件[***/jpa测试/dl/目标/类/公共/全局属性存储库.class]

此外,由于EnableJpaRepositories只扫描在其中声明的包,所以存储库没有初始化。

如果将baseClassPackage(类型安全)属性设置为EnableJpaRepositories(basePackageClasses=GlobalPropertiesRepository.class),则存储库会初始化。还要确保添加EntityScan(basePackageClasses=GlobalPropertiesDAO.class)

 类似资料:
  • 问题内容: 我有多个Node应用程序(在Express框架上构建)。 现在,我将它们这样放置- 现在,我想在同一端口(例如8080)上运行这3个应用程序。那可能吗 ? 需要注意的一件事是,每个应用都有类似的通用路线- 基本上,我想这样做,就像您可以使用Apache / PHP设置一样。 因此,当您拥有LAMP堆栈时- 您可以通过-作为其他应用轻松访问它们- 问题答案: 您可以使用:

  • 问题内容: 我可以使用一台可以访问10个内核的机器-我想实际使用它们。我习惯在自己的机器上做的事情是这样的: 我要执行10个文件-我们将其称为blah00.fa,blah01.fa,… blah09.fa。 这种方法的问题在于,myProgram一次仅使用1个内核,并且在多核计算机上这样做,我将一次使用10个内核,因此我不会使用mahcine来其最大能力。 如何更改脚本,以使其同时运行所有10个.

  • 问题内容: 假设我在同一台计算机上同时运行两个Java程序。这些程序将在单个JVM实例中运行还是在两个不同的JVM实例中运行? 问题答案: 如果您使用命令(从命令行)开始每个命令,它们将作为完全独立的JVM运行。 “程序”可以作为在一个JVM中运行的单独线程启动。

  • spark.executor.cores=2 spark.executor.memory=10GB 现在Spark在每个worker节点上启动一个Executor的JVM,对吗? 然后,在第一个会话使用configs进行之前,启动另一个Spark应用程序/会话 JVM的开销有多大?我的意思是,在用例2中,节点的RAM被分成7个JVM时,有多少RAM不会用于计算目的?

  • 我用RabbitMQ作为消息服务器编写了一个Spring-MVC-Hibernate应用程序 该应用程序现在托管在我本地系统中的单个tomcat服务器上。 我想在多个JVM节点环境中测试我的应用程序,即在多个tomcat服务器上运行的应用程序。 测试应用程序的最佳方法是什么。 我脑海中浮现的一些事情 A、 安装 B.将应用程序托管在像OpenShift、Cloud doundry这样的PAAS上,

  • 假设我在同一个公共Docker映像上安装了一个Web服务器和一个数据库服务器,是否可以同时运行它们,就像它们在同一个虚拟机中运行一样? 它正在运行<代码>docker run吗