Spring - 从包中排除一个 bean 被扫描 [英] Spring - Excluding a bean from a package from being scanned

查看:46
本文介绍了Spring - 从包中排除一个 bean 被扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在包 com.xyz.abc 中有大约 50 个 spring beans 并且我想排除其中 2 个 bean 作为 bean 处理,有没有办法做到这一点?我正在使用 Spring Boot.

If I have about 50 spring beans in package com.xyz.abc and I want to exclude 2 of those beans from being treated as a bean , is there a way to do it? I am using Spring Boot.

@ComponentScan({'com.xyz.abc'})

有一个类 Automobile.class,我不想将其视为 Spring Bean.但是我有 Car.class 扩展汽车被视为弹簧豆.

There is a class Automobile.class which I don't want to be treated as Spring Bean. However I have Car.class which extends Automobile to be treated as spring bean.

推荐答案

您可以使用 excludeFilters @ComponentScan 注释.

这允许排除特定的类,匹配给定模式的类...

This allows for the exclusion of specific classes, classes matching a given pattern...

例如,要排除 firstClasssecondClass,您可以这样写:

For example, to exclude firstClass and secondClass you would write:

@ComponentScan(value = {'com.xyz.abc'}, excludeFilters = {
  @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = { firstClass.class, secondClass.class })
})

这篇关于Spring - 从包中排除一个 bean 被扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆