Skip to content

Reject conditional @ComponentScan declarations that rely on the REGISTER_BEAN phase #23206

Closed
@wangxing-git

Description

@wangxing-git

spring boot version

2.1.5.RELEASE

TestConfig.java

package org.xyattic.boot.calkin.demo.oauth2.server.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.Resource;

/**
 * @author wangxing
 * @create 2019/6/28
 */
@Configuration
@Import(TestConfig.Test.class)
@ConditionalOnBean(Resource.class)
public class TestConfig {

    @ConditionalOnClass(Resource.class)
    @ComponentScan("org.xyattic.boot.calkin.demo.oauth2.test")
    class Test {

    }

}

The package org.xyattic.boot.calkin.demo.oauth2.test by default is not scanned.
When I activated it under certain conditions, I found that @ComponentScan was also activated when the condition was not met.

TestConfig:
      Did not match:
         - @ConditionalOnBean (types: org.springframework.core.io.Resource; SearchStrategy: all) did not find any beans of type org.springframework.core.io.Resource (OnBeanCondition)

   TestConfig.Test:
      Did not match:
         - Ancestor org.xyattic.boot.calkin.demo.oauth2.server.config.TestConfig did not match (ConditionEvaluationReport.AncestorsMatchedCondition)
      Matched:
         - @ConditionalOnClass found required class 'org.springframework.core.io.Resource' (OnClassCondition)

The log shows as not matching.
And the following will not accidentally activate.

@Configuration
@Import(TestConfig.Test.class)
//@ConditionalOnBean(Resource.class)
@ConditionalOnProperty("test.enabled")
public class TestConfig {

    @ConditionalOnClass(Resource.class)
    @ComponentScan("org.xyattic.boot.calkin.demo.oauth2.test")
    class Test {

    }

}

Metadata

Metadata

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions