Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.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
Java 依赖项注入在基于maven的springboot多模块项目中失败_Java_Spring_Maven - Fatal编程技术网

Java 依赖项注入在基于maven的springboot多模块项目中失败

Java 依赖项注入在基于maven的springboot多模块项目中失败,java,spring,maven,Java,Spring,Maven,嗨,朋友们,我正在开发一个基于maven的spring boot项目,这个项目是多模块的,其中一个模块是主模块,第二个模块是服务模块。我在主模块中有一个控制器,在Serivce模块中有一个服务 控制器 package com.aquevix.controller; import com.aquevix.common.MyService; import org.springframework.web.bind.annotation.RequestMapping; import org.sprin

嗨,朋友们,我正在开发一个基于maven的spring boot项目,这个项目是多模块的,其中一个模块是主模块,第二个模块是服务模块。我在主模块中有一个控制器,在Serivce模块中有一个服务

控制器

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();
    }
}
服务

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";
    }
}
在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> {

}
package com.aquevix.common;
导入org.springframework.data.jpa.repository.*;
/**
*Spring数据是Book实体的JPA存储库。
*/
公共接口BookRepository扩展了JpaRepository{
}
因此,当我从main模块执行main类时,我的项目在service模块中没有bookrepository(或存在于主模块中)的情况下运行良好,但如果我将bookrepository放入service模块中,则MyController无法实例化MyController中bookrepository的依赖注入故障。
有谁能帮助我如何避免此故障吗?我在服务模块中放置了任何接口,该接口将被注入模块

您需要对存储库位置进行如下Java配置:

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

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

更多参考资料在

我有一个类似的问题,并通过这篇文章解决了它:

简而言之,将此批注添加到应用程序中:

@Import(SharedConfigurationReference.class)
然后在您的服务项目中创建
共享配置参考

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 {}

对于实体和组件扫描批注,请确保指定一个包,该包对您的所有项目都是父级的,即父级对控制器和服务。是否可以显示您的应用程序类?第二个问题——在将存储库的包重新定位到服务模块时,您是否在更改存储库的包?我在一个多模块Spring Boot项目中遇到了类似的问题,我的Mongo存储库位于不同的项目中。也尝试在Eclipse中添加作为依赖项项目,但没有成功。它在引用cglib时抛出错误。必须转换为Java独立应用程序才能工作。显示主Spring应用程序类并显示包结构。Spring Boot不需要此配置。请查看文档thnx的部分内容以获取回复,如果我的自定义接口不扩展jpa存储库,并且我想动态创建bean,该怎么办