简体   繁体   中英

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

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?

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM