扫描 Spring Boot 应用程序中不同 Maven 模块/JAR 的组件

2024-01-09

我有两个 Maven 模块。 第一个称为“应用程序”,包含spring boot仅包含这些行的应用程序类:

package org.example.application;

@SpringBootApplication
@ComponentScan({"org.example.model", "org.example"})
public class Application {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }
}

在同一个 Maven 模块和包中,org.example.application, 我有一个RestController使用一个Component它又使用下面描述的其他 Maven 模块的组件。

另一个 Maven 模块称为“model”,包含spring boot组件(crud 存储库、实体等)。所有这些类都与第一个 Maven 模块位于相同的包结构下(org.example)但是在它的子包中,比如org.example.model.entities, org.example.model.repositories etc.

所以,流程是这样的:

Maven模块application包装内组织示例:
SpringBootApplication -> RestController -> MyComponent

以及应该自动装配的组件MyComponent是那些在model包下的maven模块org.example.model.

但是当我启动应用程序时,我收到错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field myRepository in org.example.MyComponent required a bean of type 'org.example.model.repositories.MyRepository' that could not be found.

Action:

Consider defining a bean of type 'org.example.model.repositories.MyRepository' in your configuration.

org.example.model.repositories.MyRepository确实存在于 Maven 模块“model”中,但 SpringBootApplication 类找不到!

如您所见,我尝试将扫描组件显式定义为:@ComponentScan({"org.example.model", "org.example"})但这似乎没有帮助。

那么我到底做错了什么?


您应该想知道的第一件事是:为什么您要声明@ComponentScan而目标之一@SpringBootApplication是(除其他外)启用组件扫描?
From Spring Boot 文档 http://docs.spring.io/autorepo/docs/spring-boot/current/reference/html/using-boot-using-springbootapplication-annotation.html :

The @SpringBootApplication注解相当于使用@Configuration, @EnableAutoConfiguration and @ComponentScan和他们的 默认属性

请注意,在 Spring Boot 应用程序的类上,您声明@ComponentScan将值指定为basePackages,它会覆盖basePackages默认情况下使用@SpringBootApplication这是该类所在的当前包。因此,要将 Spring Boot 应用程序类的包和缺少的附加包都作为基础包,您必须显式设置它们。

Besides basePackages是递归的。因此,要启用扫描位于"org.example" and "org.example.model"包,指定"org.example"就足够了"org.example.model"是它的一个子包。

尝试一下:

@SpringBootApplication(scanBasePackages={"org.example"})

或者:

@SpringBootApplication
@ComponentScan("org.example")

当在 Spring Boot 应用程序中指定 @EnableJpaRepositories/@ComponentScan/scanBasePackages 时?

当您设计 Spring Boot 应用程序布局时,您有两种情况:

1) 使用包布局(以零配置提供 Spring Boot 自动配置)的情况(有利)。

总结一下:如果您的类使用 Spring Bean 构造型注释:@Component, @Repositories, @Repositories,...位于 Spring Boot Application 类的同一个包或子包中,仅声明@SpringBootApplication是你所需要的全部。

2)不使用提供 Spring Boot 零配置自动配置的包布局的情况(避免)。

这通常意味着您要扫描的候选类不在您的类的包(或子包)中注释为@SpringBootApplication.
在这种情况下,您添加scanBasePackages属性或添加@ComponentScan指定要扫描的包。
但此外,如果您的存储库不位于带有注释的类的包或子包中@SpringBootApplication,还必须声明其他内容,例如:@EnableJpaRepositories(="packageWhereMyRepoAreLocated")

这是文档 https://docs.spring.io/spring-boot/docs/current/reference/html/howto-data-access.html#howto-use-spring-data-repositories关于这部分(重点是我的):

80.3 使用 Spring 数据存储库

Spring Data 可以创建 @Repository 接口的实现 各种口味。Spring Boot 会为您处理所有这些,只要 这些@Repositories包含在同一个包中(或 @EnableAutoConfiguration 类的子包)。

对于许多应用程序,您所需要做的就是放置正确的 Spring Data 依赖于你的类路径(有一个 spring-boot-starter-data-jpa 用于 JPA 和 spring-boot-starter-data-mongodb for Mongodb)并创建一些 用于处理 @Entity 对象的存储库接口。例子在 JPA 示例和 Mongodb 示例。

Spring Boot 尝试猜测您的 @Repository 的位置 定义,基于它找到的@EnableAutoConfiguration。要得到 更多控制,使用 @EnableJpaRepositories 注释(来自 Spring 数据 JPA)。


Examples

1) 使用包布局(以零配置提供 Spring Boot 自动配置)的情况(有利)。

在 Spring Boot 应用程序中声明org.example包,以及在同一包或子包中声明的所有 bean 类(包括存储库)org.example,对于 Spring Boot 应用程序来说,以下声明就足够了:

package org.example;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }
}

存储库可以位于org.example.repository包例如:

package org.example.repository;

@Repository
public interface FooRepository extends  JpaRepository<Foo, Long>,  { }

and

package org.example.repository;

@Repository
public interface BarRepository extends  JpaRepository<Bar, Long>,  { }

控制器可以位于org.example.controller包裹 :

package org.example.controller;

@RestController
@RequestMapping("/api/foos")
public class FooController  {...}

所以对于...

2)不使用提供 Spring Boot 零配置自动配置的包布局的情况(避免)。

在 Spring Boot 应用程序中声明org.example.application包,而不是在同一个包或子包中声明的所有 bean 类(包括存储库)org.example.application,Spring Boot应用程序需要以下声明:

package org.example.application;

@SpringBootApplication(scanBasePackages= {
                      "org.example", 
                      "org.thirdparty.repository"})
@EnableJpaRepositories("org.thirdparty.repository")
public class Application {
    public static void main(String[] args) {
        ApplicationContext ctx = SpringApplication.run(Application.class, args);
    }
}

Bean 类可能如下所示。

可能来自外部 JAR 的存储库可能位于org.thirdparty.repository包例如:

package org.thirdparty.repository;

@Repository
public interface FooRepository extends  JpaRepository<Foo, Long>,  { }

and

package org.thirdparty.repository;

@Repository
public interface BarRepository extends  JpaRepository<Bar, Long>,  { }

控制器可以位于org.example.controller包裹 :

package org.example.controller

@RestController
@RequestMapping("/api/foos")
public class FooController  {...}

所以对于...

结论:确实鼓励在命名空间的基础包中定义 Spring Boot 应用程序,以使 Spring Boot 配置尽可能简单。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

扫描 Spring Boot 应用程序中不同 Maven 模块/JAR 的组件 的相关文章

  • 打印星号的 ASCII 菱形

    我的程序打印出这样的钻石 但只有当参数或菱形的每一面为4 例如如果我输入6 底部三角形的间距是错误的 我一直在试图找出答案 当参数改变时 底部的三角形不会改变 只有顶部的三角形会改变 它只适用于输入4 public static void
  • 不同类型的数组

    是否可以有一个包含两种不同类型数据的数组 我想要一个包含双精度型和字符串的数组 我尝试过 ArrayList
  • 大数据使用什么数据结构

    我有一个包含一百万行的 Excel 工作表 每行有 100 列 每行代表一个具有 100 个属性的类的实例 列值是这些属性的值 哪种数据结构最适合在这里使用来存储数百万个数据实例 Thanks 这实际上取决于您需要如何访问这些数据以及您想要
  • 如何在代理后面安装 Eclipse Neon

    对于 Neon Eclipse 附带了一个安装程序 我在安装程序中找不到任何配置菜单 我的java版本是 java version java version 1 8 0 72 Java TM SE Runtime Environment b
  • 是否可以通过编程方式查找 logback 日志文件?

    自动附加日志文件以支持电子邮件会很有用 我可以以编程方式设置路径 如以编程方式设置 Logback Appender 路径 https stackoverflow com questions 3803184 setting logback
  • 如何检测 Java 字符串中的 unicode 字符?

    假设我有一个包含 的字符串 我如何找到所有这些 un icode 字符 我应该测试他们的代码吗 我该怎么做呢 例如 给定字符串 A X 我想将其转换为 AYXY 我想对其他 unicode 字符做同样的事情 并且我不想将它们存储在某种翻译映
  • 合并来自多个 spring-boot YAML 文件的列表

    是否可以组合使用 YAML 编写的多个 spring boot 配置文件中相同元素的列表 Example postgres yml包含我的 postgres 数据库信息 它还包含我的迁移脚本的飞行路径位置 flyway locations
  • 从 GitHub 上托管的 Spring Cloud Config Server 访问存储库的身份验证问题

    我在 GitHub 上的存储库中托管配置 如果我将回购公开 一切都好 但如果我将其设为私有 我将面临 org eclipse jgit errors TransportException https github com my user m
  • @Autowire注释的问题(空)

    我在验证器类中自动连接的两个服务有问题 这些服务工作正常 因为在我的控制器中是自动连接的 我有一个 applicationContext xml 文件和 MyApp servlet xml 文件 我的基础包是 es unican meteo
  • 使用架构注册表对 avro 消息进行 Spring 云合约测试

    我正在查看 spring 文档和 spring github 我可以看到一些非常基本的内容examples https github com spring cloud samples spring cloud contract sample
  • QuerySyntaxException:无法找到类

    我正在使用 hql 生成 JunctionManagementListDto 类的实际 Java 对象 但我最终在控制台上出现以下异常 org hibernate hql internal ast QuerySyntaxException
  • 如何在 Java 中创建接受多个值的单个注释

    我有一个名为 Retention RetentionPolicy SOURCE Target ElementType METHOD public interface JIRA The Key Bug number JIRA referenc
  • 是否可以使用 Java Guava 将函数应用于集合?

    我想使用 Guava 将函数应用于集合 地图等 基本上 我需要调整 a 的行和列的大小Table分别使所有行和列的大小相同 执行如下操作 Table
  • “无法实例化活动”错误

    我的一个 Android 应用程序拥有大约 100 000 个用户 每周大约 10 次 我会通过 Google 的市场工具向我报告以下异常情况 java lang RuntimeException Unable to instantiate
  • Java Swing:需要一个高质量的带有复选框的开发 JTree

    我一直在寻找一个 Tree 实现 其中包含复选框 其中 当您选择一个节点时 树中的所有后继节点都会被自动选择 当您取消选择一个节点时 树中其所有后继节点都会自动取消选择 当已经选择了父节点 并且从其后继之一中删除了选择时 节点颜色将发生变化
  • Resteasy 可以查看 JAX-RS 方法的参数类型吗?

    我们使用 Resteasy 3 0 9 作为 JAX RS Web 服务 最近切换到 3 0 19 我们开始看到很多RESTEASY002142 Multiple resource methods match request警告 例如 我们
  • Android:无法发送http post

    我一直在绞尽脑汁试图弄清楚如何在 Android 中发送 post 方法 这就是我的代码的样子 public class HomeActivity extends Activity implements OnClickListener pr
  • 将 Apache Camel 执行器指标发送到 Prometheus

    我正在尝试转发 添加 Actuator Camel 指标 actuator camelroutes 将交换 交易数量等指标 发送到 Prometheus Actuator 端点 有没有办法让我配置 Camel 将这些指标添加到 Promet
  • 配置“DataSource”以使用 SSL/TLS 加密连接到 Digital Ocean 上的托管 Postgres 服务器

    我正在尝试托管数据库服务 https www digitalocean com products managed databases on 数字海洋网 https en wikipedia org wiki DigitalOcean 创建了
  • Java EE 目录结构

    我对以下教程有疑问 http www mkyong com jsf2 jsf 2 internationalization example http www mkyong com jsf2 jsf 2 internationalizatio

随机推荐