简体   繁体   English

spring boot中依赖注入失败,基于maven的多模块项目

[英]Dependency Injection failed in spring boot , maven based multiple modules project

Hi friends i am developing a maven based spring boot project project , this project is multiple module form one module is Main module and second module is Service Module.嗨朋友们,我正在开发一个基于 maven 的 Spring Boot 项目项目,这个项目是多个模块,一个模块是模块,第二个模块是服务模块。 I have one controller in Main module and one service in Serivce module我在 Main 模块中有一个控制器,在 Serivce 模块中有一个服务

Controller控制器

package com.aquevix.controller;

import com.aquevix.common.MyService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.inject.Inject;

/**
 * Created by mohdqasim on 11/9/15.
 */
@RestController
@RequestMapping("/api")
public class MyController {

    @Inject MyService myService;
    @Inject BookRepository bookRepository;
    @RequestMapping(value = "/data" , method = RequestMethod.GET)
    public String getData(){
     return myService.getData();
    }
}

Service服务

package com.aquevix.common;

import org.springframework.stereotype.Service;

/**
 * Created by mohdqasim on 11/9/15.
 */
@Service
public class MyService {
    public String getData(){
        return "hello qasim";
    }
}

In maven multiple modules this scenerio is working fine but i have also one repository in the form of interface in service module.在 maven 多个模块中,这个场景运行良好,但我在服务模块中也有一个接口形式的存储库。

package com.aquevix.common;

import org.springframework.data.jpa.repository.*;

/**
 * Spring Data JPA repository for the Book entity.
 */
public interface BookRepository extends JpaRepository<Book,Long> {

}

So when i execute main class from Main module my project works fine without bookrepository in service module( or present in Main module) but if i put bookrepository in Service module then MyController could not instantiate due dependency injection failure of bookRepository in MyController.因此,当我从Main模块执行 main 类时,我的项目在服务模块(或存在于 Main 模块)中没有 bookrepository 的情况下工作正常,但是如果我将 bookrepository 放在Service模块中,那么 MyController 无法实例化 MyController 中由于 bookRepository 的依赖注入失败。 Can anyone help me how to to avoid this failure i put any interface in Service module which is being injected into Main module谁能帮助我如何避免这种失败我把服务模块的所有接口,它被注入到模块

You would need to JavaConfig your repository location like below:您需要 JavaConfig 您的存储库位置,如下所示:

@Configuration
@EnableJpaRepositories("com.aquevix.common")
class ApplicationConfiguration {

  @Bean
  public EntityManagerFactory entityManagerFactory() {
    // …
  }
}

More reference at Working with Spring Data Repositories更多参考使用 Spring Data Repositories

I had a similar problem and solved it using this post: Spring Boot: autowire beans from library project我有一个类似的问题,并使用这篇文章解决了它: Spring Boot: autowire beans from library project

In short, add this annotation to your application:简而言之,将此注释添加到您的应用程序中:

@Import(SharedConfigurationReference.class)

And then in your Service project create the SharedConfigurationReference class然后在您的服务项目中创建SharedConfigurationReference

package com.aquevix.common;

import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@Configuration
@ComponentScan("com.aquevix")
@EnableJpaRepositories("com.aquevix")
@EntityScan("com.aquevix")
public class SharedConfigurationReference {}

For the entity and component scan annotations, make sure to specify a package that is parent to all your projects, ie parent to controller and service对于实体和组件扫描注释,请确保指定所有项目的包,即控制器和服务的父包

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM