Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Hibernate的Spring引导-来自另一个项目的自动布线存储库_Spring_Hibernate_Spring Boot_Spring Data Jpa_Autowired - Fatal编程技术网

使用Hibernate的Spring引导-来自另一个项目的自动布线存储库

使用Hibernate的Spring引导-来自另一个项目的自动布线存储库,spring,hibernate,spring-boot,spring-data-jpa,autowired,Spring,Hibernate,Spring Boot,Spring Data Jpa,Autowired,我正试图建立两个独立的项目: myApplicationWorker:后端服务器逻辑 myApplicationCommon:包含将在myApplicationWorker和未来项目之间共享的模型和其他逻辑 我的myApplicationWorker的build.gradle依赖项如下所示: dependencies { compile('org.springframework.boot:spring-boot-starter') compile project(':common

我正试图建立两个独立的项目:

  • myApplicationWorker:后端服务器逻辑
  • myApplicationCommon:包含将在myApplicationWorker和未来项目之间共享的模型和其他逻辑
  • 我的myApplicationWorker的build.gradle依赖项如下所示:

    dependencies {
        compile('org.springframework.boot:spring-boot-starter')
        compile project(':common')
        testCompile('org.springframework.boot:spring-boot-starter-test')
    }
    
    dependencies {
        compile('org.springframework.boot:spring-boot-starter-data-jpa')
        compile('mysql:mysql-connector-java')
        compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.9.1'
        compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.9.1'
        compile('org.apache.commons:commons-text:1.1')
        testCompile("com.h2database:h2")
        testCompile('junit:junit:5.0.2')
        testCompile('org.mockito.mockito-core:2.12.0')
    }
    
    我的myApplicationCommonbuild.gradle依赖项如下所示:

    dependencies {
        compile('org.springframework.boot:spring-boot-starter')
        compile project(':common')
        testCompile('org.springframework.boot:spring-boot-starter-test')
    }
    
    dependencies {
        compile('org.springframework.boot:spring-boot-starter-data-jpa')
        compile('mysql:mysql-connector-java')
        compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.9.1'
        compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.9.1'
        compile('org.apache.commons:commons-text:1.1')
        testCompile("com.h2database:h2")
        testCompile('junit:junit:5.0.2')
        testCompile('org.mockito.mockito-core:2.12.0')
    }
    
    (包含测试/模拟/日志依赖项以及JPA/MySQL依赖项。)

    简单的hibernate/数据源属性在应用程序中定义。属性

    spring.jpa.hibernate.ddl-auto=none
    spring.datasource.url=url
    spring.datasource.username=username
    spring.datasource.password=password
    
    我定义了一个“帐户””模型:

    package com.myapplication.common.models;
    
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;
    
    @Entity
    @Table(name = "accounts")
    public class Account {
        @Id
        private long id;
    
        //Other variables, getters/setters omitted for length
    
    账户模型有一个账户存储库

    package com.myapplication.common.models.repositories;
    
    import com.myapplication.common.models.Account;
    import org.springframework.data.repository.CrudRepository;
    import org.springframework.stereotype.Repository;
    
    @Repository
    public interface AccountRepository extends CrudRepository<Account, Long> {
        Account findAccountById(long id);
    }
    
    当我尝试在testPrintAccountId()中与accountRepository交互时,这给了我一个NPE,但是,当我手动初始化上下文时,它(显然)起作用:

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class);
        AccountRepository accountRepository = context.getBean(AccountRepository .class);
        System.out.println(accountRepository .findById(1234).getAccountId());
    
        (Successfully returns result)
    }
    

    我显然误解了弹簧和弹簧靴在这里的工作原理。如何实现将公共项目中的存储库自动关联到其他项目中的目标?

    如果您希望自动关联主类中的依赖项。Spring尚未将依赖项自动连接到App对象中。如果要在Spring完成创建和自动关联后调用此方法,请添加带有@PostConstruct注释的方法,例如:

    @SpringBootApplication
    public class Application {
        @Autowired
        private AccountRepository accountRepository;
    
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    
    
        @PostConstruct
        public void testPrintAccountId() {
            System.out.println(accountRepository.findAccountbyId(1).getAccountId());
        }
    }
    

    或者您可以在@Component类中使用它,比如@Controller@Service

    ,以防有人来这里,就像我做的那样

    这是可能的,因此OP可能还有其他NPE问题。以下是在非核心模块/应用程序中对我有用的内容:

    @SpringBootApplication
    @EnableJpaRepositories("com.package.domain.respositories")
    @ComponentScan("com.package.domain")
    public class ClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ClientApplication.class, args);
        }
    
    }
    
    测试运行正常,ClientApp从域包中加载存储库,没有问题