带有 Spring Boot 中所需的显式 basePackages 的 IntegrationComponentScan?

     2023-02-26     239

关键词:

【中文标题】带有 Spring Boot 中所需的显式 basePackages 的 IntegrationComponentScan?【英文标题】:IntegrationComponentScan with an explicit basePackages required in Spring Boot? 【发布时间】:2021-12-07 12:20:32 【问题描述】:

我有一个使用 Spring 集成的 Spring Boot 应用程序(使用 Spring Boot 2.4.11)。我添加了org.springframework.boot:spring-boot-starter-integration,因此我希望@IntegrationComponentScan 默认处于活动状态。事实上,我发现Issue 2037 似乎暗示了这一点。另外,我在org.springframework.boot.autoconfigure.integration.IntegrationAutoConfigurationScanRegistrar.IntegrationComponentScanConfiguration 上看到了该注释。

但是,我的 @MessagingGateway 注释接口没有被 Spring 占用。我试图在我的一个 @Configuration 类中添加一个显式的 @IntegrationComponentScan,但它也不起作用。

一旦我还为该注释指定了明确的 basePackages 值,它就开始工作了,所以:

@Configuration
@IntegrationComponentScan(basePackages = "com.example.app.integration")
public class MyConfigurationClass 

当然我的注释接口在com.example.app.integration下面的一个包中。

我错过了什么吗?我在 Spring Boot 文档中找不到对 @IntegrationComponentScan 的任何引用。其他类似的注解(如 @EnableJpaRepositories 用于 Spring Data 或 @EntityScan)并不是绝对必要的,除非您需要自定义扫描范围。

我还找到了Issue 3375,这似乎表明我应该看看@AutoConfigurationPackage,但是:

我在 Spring Boot 文档中找不到任何提及此注释的内容 我怀疑这将适用于 Spring Boot 启用的任何其他“扫描”注释,因此在这种情况下,我的 basePackages 可能指向一些非常接近我的应用程序“根”的包 为什么其他@*Scan 注释不需要这个注释?

【问题讨论】:

【参考方案1】:

我认为您需要展示您的项目结构。它按预期工作:

    我有一个类似com.example.integration.gateway.MyGateway的界面

    我的主要课程在com.example.integration.IntegrationApplication,内容如下:

    @SpringBootApplication
    public class IntegrationApplication 
    

即使我在 com.example.integration 包中有 MyGateway,它仍然有效。

总结。 @SpringBootApplication@ComponentScan 标记,Spring 开始从声明了 @SpringBootApplication 的包中扫描组件,一直扫描到它的子包。

如果您的组件与 @SpringBootApplication 类不同,请考虑通过其 scanBasePackages 属性对其进行自定义:

/**
 * Base packages to scan for annotated components. Use @link #scanBasePackageClasses
 * for a type-safe alternative to String-based package names.
 * <p>
 * <strong>Note:</strong> this setting is an alias for
 * @link ComponentScan @ComponentScan only. It has no effect on @code @Entity
 * scanning or Spring Data @link Repository scanning. For those you should add
 * @link org.springframework.boot.autoconfigure.domain.EntityScan @EntityScan and
 * @code @Enable...Repositories annotations.
 * @return base packages to scan
 * @since 1.3.0
 */
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default ;

您的项目中的所有其他内容都需要进行调查。如果您可以与我们分享,我们可以看看并找到一些东西。

【讨论】:

正确使用函数的显式模板实例化?

...制使用所需的数据类型。我真的不知道该怎么做。有一个带有以下函数的标题和一个具有相同语法的第二个函数(如果相关,还有相同的模板名称?):temp 查看详情

使用带有显式配置的 Spring Boot

】使用带有显式配置的SpringBoot【英文标题】:UseSpringBootwithexplicitlyconfiguration【发布时间】:2019-09-1621:17:29【问题描述】:是否可以使用SpringBoot使所有配置都显式在主类中?例如,是否可以告诉spring-boot打印@SpringBootApplication所做... 查看详情

Spring Boot 中不存在所需的请求部分“文件”

】SpringBoot中不存在所需的请求部分“文件”【英文标题】:Requiredrequestpart\'file\'isnotpresentinSpringBoot【发布时间】:2020-10-2207:00:59【问题描述】:我检查了所有类似的帖子,但仍然找不到解决方案。问题是所需的请求部分“文件... 查看详情

设计数据访问对象 (DAO) 层所需的说明 - Spring Boot

】设计数据访问对象(DAO)层所需的说明-SpringBoot【英文标题】:ClarificationsneededonDesigningDataAccessObject(DAO)Layer-SpringBoot【发布时间】:2021-08-2705:04:14【问题描述】:我正在尝试为SpringBoot项目设置DAO层,我遇到了这个helpfulblog。我需要... 查看详情

Spring-Boot REST - 所需的字符串参数不存在

】Spring-BootREST-所需的字符串参数不存在【英文标题】:Spring-BootREST-RequiredStringparameterisnotpresent【发布时间】:2016-09-2102:29:57【问题描述】:所以,我正在尝试向本地SpringMVC项目发送POST请求,但每次尝试时,我都会收到400:BadReques... 查看详情

在 tomcat 中运行的 Spring Boot,所需的请求部分“文件”不存在

】在tomcat中运行的SpringBoot,所需的请求部分“文件”不存在【英文标题】:Springbootrunningintomcat,requiredrequestpart\'file\'isnotpresent【发布时间】:2021-11-0808:43:12【问题描述】:过去2天我一直在阅读堆栈溢出问题的解决方案。我已经... 查看详情

MSBuild 的 Visual Studio 解决方案项目中所需的更改

...ild【发布时间】:2019-03-0220:44:22【问题描述】:我有一个带有两个简单类库项目的.Net解决方案。这两个项目都包含一个方法,例如helloworld。解决方案正在使用VisualStudio和MSBuild.exe正确构建。现在,当我将P 查看详情

我应该如何在 Spring Boot 中配置 httpMessageConverters 以将消息转换为所需的格式?

】我应该如何在SpringBoot中配置httpMessageConverters以将消息转换为所需的格式?【英文标题】:HowshouldIconfigurehttpMessageConvertersinSpringboottoconvertmessagesintorequiredformat?【发布时间】:2016-08-2115:32:09【问题描述】:我使用SpringBoot编写了一... 查看详情

表单提交错误,无法将类型“java.lang.String”的值转换为浏览器中所需的类型错误,在 Spring MVC 中

...java.lang.String”的值转换为浏览器中所需的类型错误,在SpringMVC中【英文标题】:Formsubmiterror,Failedtoconvertvalueoftype\'java.lang.String\'torequiredtypeerrorinbrowser,InspringMVC【发布时间】:2016-02-1803:14:14【问题描述】:所以,我正在尝试使用sp... 查看详情

Sql - WHERE 条件的显式顺序?

...句的所有部分以确定满足查询的最有效方法,如这些答案中所述:DoesorderofwhereclausesmatterinSQLSQL-DoestheorderofWHEREco 查看详情

Spring Boot 和 HTML 方法参数类型字符串所需的请求参数“Coord1”不存在]

】SpringBoot和HTML方法参数类型字符串所需的请求参数“Coord1”不存在]【英文标题】:SpringBootandHTMLRequiredrequestparameter\'Coord1\'formethodparametertypeStringisnotpresent]【发布时间】:2021-09-2123:55:48【问题描述】:我刚刚开始使用Html+springboot... 查看详情

Blazor 组件中所需的级联参数

】Blazor组件中所需的级联参数【英文标题】:RequiredcascadingparametersinBlazorcomponents【发布时间】:2022-01-2021:14:37【问题描述】:[Parameter,EditorRequired]publicintParameterget;set;=default!;[CascadingParameter,EditorRequired]publicintParameter2get 查看详情

webpack 包中所需的模块未定义

】webpack包中所需的模块未定义【英文标题】:requiredmodulesinawebpackbundleareundefined【发布时间】:2016-09-1614:19:51【问题描述】:我正在构建一个javascript库并使用require()方法将外部模块要求到其他模块中。我的问题是我需要的模块没... 查看详情

我想加载一个基于标志的特定类,而不是加载两个类并使用 Springs 中所需的一个

...想加载一个基于标志的特定类,而不是加载两个类并使用Springs中所需的一个【英文标题】:IwanttoloadaspecificclassbasedonaflaginsteadofloadingtwoclassandusingonetherequiredinSprings【发布时间】:2018-07-1818:58:58【问题描述】:在配置中我有一个标... 查看详情

如何自动滚动到文本中所需的单词

】如何自动滚动到文本中所需的单词【英文标题】:Howtomakeautomaticscrollingtothedesiredwordinthetext【发布时间】:2018-10-1115:47:08【问题描述】:当我从搜索移动到文本区域中存在大文本的所需单词时,我已经有了这个单词的第一个索... 查看详情

从 Electron 中所需的 JS 模块访问父变量

】从Electron中所需的JS模块访问父变量【英文标题】:AccessparentvariablesfromrequiredJSModulesinElectron【发布时间】:2020-08-0617:55:32【问题描述】:请耐心等待,这里有点二人组。首先,我认为如果我的主要JS像这样设置是正确的..constmyMo... 查看详情

使接受 Laravel 中所需的条款和条件字段

】使接受Laravel中所需的条款和条件字段【英文标题】:MakeAcceptTermsandconditionsfieldsrequiredinLaravel【发布时间】:2021-10-2904:47:34【问题描述】:我在vue.js中有一个复选框<inputv-model="form.accept"name="Accept"type="checkbox"value="false">我已... 查看详情

如何使用 Thymeleaf 的显式链接?

...eleaf?【发布时间】:2021-08-1415:07:15【问题描述】:我想在Spring+ThymeleafWeb应用程序中创建并链接到外部站点并插入如下链接:&lt;ath:href="@https://example.com"&gt;Link&lt;/a&gt;但是 查看详情