当所有bean都标记为Lazy时,Spring Boot应用程序无法启动,因为它找不到错误通道

在Spring Boot 2.2中,您可以默认将所有bean标记为惰性。

如果我通过以下方式打开

spring.main.lazy-initialization=true

我收到以下消息:

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

Description:

A component required a bean named 'errorChannel' that could not be found.

引用此代码的相关代码为:

@MessagingGateway(errorChannel = "errorChannel")
@FunctionalInterface
public interface SomeInterface {

惰性设置似乎已阻止Spring Integration创建errorChannel。

如何在这里将errorChannel bean标记为不懒惰?

我又如何在Spring Boot 2.2中默认情况下将其他类从懒惰中排除呢?

kk771396 回答:当所有bean都标记为Lazy时,Spring Boot应用程序无法启动,因为它找不到错误通道

嗯,虽然我在其他地方也遇到了一些与懒惰相关的异常。 我想您的申请比您在问题中说的要复杂得多。

反正有LazyInitializationExcludeFilter

@Bean
public static LazyInitializationExcludeFilter integrationExcludeFilter() {
    return (beanName,beanDefinition,beanType) -> "testChannel".equals(beanName);
}

查看其JavaDocs:

/**
 * Filter that can be used to exclude beans definitions from having their
 * {@link AbstractBeanDefinition#setLazyInit(boolean) lazy-init} set by the
 * {@link LazyInitializationBeanFactoryPostProcessor}.
 * <p>
 * Primarily intended to allow downstream projects to deal with edge-cases in which it is
 * not easy to support lazy-loading (such as in DSLs that dynamically create additional
 * beans). Adding an instance of this filter to the application context can be used for
 * these edge cases.
 * <p>
 * A typical example would be something like this:
 * <p>
 * <pre><code>
 * &#64;Bean
 * public static LazyInitializationExcludeFilter integrationLazyInitializationExcludeFilter() {
 *   return LazyInitializationExcludeFilter.forBeanTypes(IntegrationFlow.class);
 * }</code></pre>
 * <p>
 * NOTE: Beans of this type will be instantiated very early in the spring application
 * lifecycle so they should generally be declared static and not have any dependencies.
 *
 * @author Tyler Van Gorder
 * @author Philip Webb
 * @since 2.2.0
 */
@FunctionalInterface
public interface LazyInitializationExcludeFilter {

从框架的角度来看,我还不知道如何解决它...

本文链接:https://www.f2er.com/3139112.html

大家都在问