【问题标题】:Springboot multimodule projectSpring Boot 多模块项目
【发布时间】:2018-10-07 05:17:30
【问题描述】:

我正在尝试获得一个干净的 springboot maven 多模块项目。 我正在使用 springboot 2.0.1.RELEASE

我想要实现的和这个类似:SpringBootMultipleMavenModules

我遇到的问题是我希望能够在任何模块中注入我的依赖项。

例如在这个类中:DBSeeder.java 如下所示:

private HotelRepository hotelRepository;

public DbSeeder(HotelRepository hotelRepository){
    this.hotelRepository = hotelRepository;
}
..

我想改用:

@Autowired
private HotelRepository hotelRepository;

Application 类如下所示:

@SpringBootApplication
@EnableJpaRepositories(basePackages = {"rc"})
@EntityScan(basePackages = {"rc"})
@ComponentScan(basePackages = {"rc"})
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

欢迎任何可以将我与解决方案联系起来的想法。

【问题讨论】:

  • 在这里查看我的回复:stackoverflow.com/questions/50043699/…
  • @MagdKudama 不幸的是,这行不通,已经尝试过
  • 什么是行不通的?
  • @MagdKudama Spring 会抱怨:rc.persistence.DbSeeder 中的 Field marriot 需要一个找不到的 'rc.domain.Hotel' 类型的 bean。
  • 我什至没有在你的代码中看到rc.domain.Hotel。它是否包含在组件扫描(或实体,取决于)中?您的 DbSeeder 类是 bean 吗?尝试将其标记为@Component

标签: spring-boot dependency-injection maven-3 multi-module


【解决方案1】:

查看您的代码,您不能 Autowire 一个 Hotel bean,因为它没有正确注册。

https://github.com/IDCS1426/SpringBootMultipleMavenModules/blob/master/domain/src/main/java/rc/domain/Hotel.java#L10

您需要在此处添加@Component 才能将其注入https://github.com/IDCS1426/SpringBootMultipleMavenModules/blob/master/persistence/src/main/java/rc/persistence/DbSeeder.java#L21

此外,该项目永远不会编译,因为您要添加一个不存在的模块:https://github.com/IDCS1426/SpringBootMultipleMavenModules/blob/master/pom.xml#L14。你需要删除它:)。

说了这么多,我觉得你尝试注入这样的Entity 的方式很奇怪,但这不是这个问题的一部分。

通过这样做,代码编译得很好。

【讨论】:

  • 感谢您的回复。我在更改代码的过程中提交了该应用程序。所以github中的评论(不会注入)。如果我在要求审查时提交了不稳定的版本,请致歉。我将在今天晚些时候提交一个合适的稳定版本。
  • 它是一个实验室。我将通过 repo 解决实体
【解决方案2】:

工作解决方案可用here。实体中缺少@Component。 显然,bean 不应该像这里那样被注入,而是实例化(例如marriot = new Hotel("Marriot", 5, true);)并通过save 方法(或saveAll 用于集合)持久化

仅出于初始化目的而注入实体是错误的,并且不起作用: 每个酒店都将重复使用相同的实例。

@Autowired
private Hotel marriot, ibis, goldenTulip;

@Override
public void run(String... strings) throws Exception {

    marriot.setName("Marriot");
    marriot.setClassification(5);
    marriot.setOpen(true);

    ibis.setName("Ibis");
    ibis.setClassification(3);
    ibis.setOpen(false);

    goldenTulip.setName("Golden Tulip");
    goldenTulip.setClassification(4);
    goldenTulip.setOpen(true);

    List<Hotel> hotels = new ArrayList<>();
    hotels.add(marriot);
    hotels.add(ibis);
    hotels.add(goldenTulip);

    this.hotelRepository.saveAll(hotels);
}

将导致一个实体持久化,因为所有 3 家酒店都是同一个实例。因此,http://localhost:8080/hotels 将返回:

[{"id":1,"name":"Golden Tulip","classification":4,"open":true}]

虽然它与实例化,

@Override
public void run(String... strings) throws Exception {
    marriot = new Hotel("Marriot", 5, true);
    ibis = new Hotel("Ibis", 3, false);
    goldenTulip = new Hotel("Golden Tulip", 4, true);

    List<Hotel> hotels = new ArrayList<>();
    hotels.add(marriot);
    hotels.add(ibis);
    hotels.add(goldenTulip);

    this.hotelRepository.saveAll(hotels);
}

它将返回 3 个实体:

[{"id":1,"name":"Marriot","classification":5,"open":true},{"id":2,"name":"Ibis","classification":3,"open":false},{"id":3,"name":"Golden Tulip","classification":4,"open":true}]

这正是我想在这里看到的,但忘记在 Entity 类中添加 @Component。千万不要那样做!

编辑: 尝试这样做的原因是使用了服务层:

public interface NoteService {
    List<Note> loadAll();
}
@Service
public class NoteServiceImpl implements NoteService {

  @Autowired
  private NoteRepository noteRepository;

  @Override
  public List<Note> loadAll() {
    return noteRepository.findAll();
  }
}

最终在运行时失败,Note 不是托管 bean。

【讨论】:

    猜你喜欢
    • 2015-10-10
    • 1970-01-01
    • 2014-06-02
    • 2016-03-27
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 2019-03-03
    • 2019-05-24
    相关资源
    最近更新 更多