Spring Boot app fails to start when all beans are marked as Lazy, as it can't find an error channel

∥☆過路亽.° 提交于 2021-01-27 21:29:38

问题


In Spring Boot 2.2, you can mark all beans as being lazy by default.

If I turn this on via

spring.main.lazy-initialization=true

I get the following message:

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

Description:

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

The code in question that references this is:

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

It looks like the lazy setting has stopped Spring Integration from creating the errorChannel.

How can I mark the errorChannel bean as being not lazy here ?

Also how can I exclude other classes from not being Lazy by default in Spring Boot 2.2 ?


回答1:


Well, I get some lazy-related exception in other place though. I guess your application is much complicated than you say in the question.

Anyway there is a LazyInitializationExcludeFilter:

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

See its 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 {

I don't know yet how to fix it from the framework perspective...



来源:https://stackoverflow.com/questions/58763720/spring-boot-app-fails-to-start-when-all-beans-are-marked-as-lazy-as-it-cant-fi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!