Spring Boot @ComponentScan exclude 不起作用

Spring Boot About 723 words

现象

使用了@ComponentScanexcludeFilters但不起作用。

示例代码

@ComponentScan(excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = TestComponent.class))
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

原因

有其他@Configuration使用@Bean注入了该对象。

解决

使用@SpringBootApplicationexclude属性,移除注入的@Bean对象。

@ComponentScan(excludeFilters = @Filter(type = FilterType.ASSIGNABLE_TYPE, classes = TestComponent.class))
@SpringBootApplication(exclude = TestConfig.class)
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

参考

https://www.baeldung.com/spring-componentscan-filter-type

Views: 2,737 · Posted: 2022-10-19

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh