首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Spring应用程序中扫描不同maven模块/JAR的组件

问在Spring应用程序中扫描不同maven模块/JAR的组件
EN

Stack Overflow用户
提问于 2017-01-02 16:12:52
回答 1查看 34.6K关注 0票数 28

我有两个Maven模块。第一个名为" Application“的类包含仅包含以下行的spring boot应用程序类:

代码语言:javascript
复制
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模块包含spring boot组件(CRUD-存储库、实体等)。所有这些类都与第一个Maven模块(org.example)的包结构相同,但它们的子包中有org.example.model.entities、org.example.model.repositories等。

所以,流程是这样的:

软件包application中的Maven模块org.example

SpringBootApplication -> RestController -> MyComponent

在MyComponent中应该自动处理的组件是包org.example.model下的model Maven模块中的组件。

但是,当我启动应用程序时,我只会得到一个错误:

代码语言:javascript
复制
***************************
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模块“模型”中,但是SpringBootApplication类找不到!

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

那我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-02 16:18:28

首先您应该感到奇怪的是:为什么您声明@ComponentScan,而@SpringBootApplication的目标之一是(以及其他事情)启用组件扫描?

来自弹簧启动文件:

@SpringBootApplication注释相当于使用@Configuration、@EnableAutoConfiguration和@ComponentScan的默认属性。

请注意,当您在Spring的类上声明@ComponentScan以将一个值指定为basePackages时,它将覆盖@SpringBootApplication默认使用的basePackages (即该类所在的当前包)。因此,要将类的包和缺少的附加包作为基本包,您必须显式地设置它们。

此外,basePackages是递归的。因此,要同时扫描位于"org.example"包和"org.example.model"包中的类,指定"org.example"就足够了,因为"org.example.model"是它的一个子包。

试一试:

代码语言:javascript
复制
@SpringBootApplication(scanBasePackages={"org.example"})

或者另一种选择:

代码语言:javascript
复制
@SpringBootApplication
@ComponentScan("org.example")

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

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

1)在这种情况下,您使用的包布局为零配置的Spring提供了自动配置。

总结一下:如果您的类注释了Spring原型:@Component、@Repositories、@Repositories,.位于Spring类的相同包或子包中,只声明@SpringBootApplication即可。

2) (为了避免这种情况),您没有使用提供零配置的Spring自动配置的包布局.

这通常意味着要扫描未包含在带有@SpringBootApplication注释的类的包(或子包)中的候选类。

在这种情况下,可以添加scanBasePackages属性或添加@ComponentScan来指定要扫描的包。

但是,如果您的存储库不在带有@SpringBootApplication注释的类的包或子包中,则必须声明其他内容,如:@EnableJpaRepositories(="packageWhereMyRepoAreLocated")。

关于这一部分的这是文件 (重点是我的):

80.3使用Spring数据存储库 Spring数据可以创建各种类型的@Repository接口的实现。Spring为您处理所有这些问题,只要@EnableAutoConfiguration类的相同包(或子包)中包含了这些@存储库。。 对于许多应用程序,您只需要在类路径上放置正确的Spring数据依赖项(对于JPA有一个spring-boot-starter JPA,对于Mongodb有一个spring-boot-starter Mongodb),并创建一些存储库接口来处理@Entity对象。示例出现在JPA示例和Mongodb示例中。 Spring Boot试图根据@EnableAutoConfiguration找到@Repository定义的位置。要获得更多的控制,请使用@EnableJpaRepositories注释(来自Spring ).

示例

1)在这种情况下,您使用的包布局为零配置的Spring提供了自动配置。

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

代码语言:javascript
复制
package org.example;

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

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

代码语言:javascript
复制
package org.example.repository;

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

和

代码语言:javascript
复制
package org.example.repository;

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

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

代码语言:javascript
复制
package org.example.controller;

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

所以对于..。

2) (为了避免这种情况),您没有使用提供零配置的Spring自动配置的包布局.

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

代码语言:javascript
复制
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包中,如:

代码语言:javascript
复制
package org.thirdparty.repository;

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

和

代码语言:javascript
复制
package org.thirdparty.repository;

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

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

代码语言:javascript
复制
package org.example.controller

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

所以对于..。

结论:真正鼓励在名称空间的基本包中定义Spring应用程序,以使Spring配置尽可能简单。

票数 39
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41430051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档