java 如何在applicationContext中从Spring中排除一个类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13034515/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:15:56  来源:igfitidea点击:

How to exclude a class from Spring in applicationContext?

javaspring

提问by Dreamer

Here we only want to exclude a class from certain classpath, say

这里我们只想从某个类路径中排除一个类,比如

com.abc.projectA.service.orderService.sectionA.orderService.class

com.abc.projectA.service.orderService.sectionA.orderService.class

However there is another class with same name but in different classpath

但是,还有另一个具有相同名称但在不同类路径中的类

com.abc.projectA.service.orderService.sectionB.orderService.class

com.abc.projectA.service.orderService.sectionB.orderService.class

so that only filer by class name is not going to work.

这样只有按类名的文件管理器是行不通的。

But I tried the following method:

但我尝试了以下方法:

<context:component-scan base-package="com.abc">
    <!--other filters-->
    <!--.......-->
    <context:exclude-filter expression="projectA\.service\.orderService\.sectionA\.orderService" type="regex" />
</context:component-scan>

It doesn't work. So I bet the <context:exclude-filter>only valid on package level but not for specific class? If so, how to exclude a class from bean injection so that we can pick and choose with class to get wired with same class name?

它不起作用。所以我打赌<context:exclude-filter>唯一在包级别有效但不适用于特定课程?如果是这样,如何从 bean 注入中排除一个类,以便我们可以选择使用类来连接相同的类名?

Thanks in advance.

提前致谢。

回答by Biju Kunjummen

No, the exclude should work, the issue that you are having is probably that you are assuming that the path in regex will be pre-pended with the base-package which is not true..so just specify the full package

不,排除应该可以工作,您遇到的问题可能是您假设正则表达式中的路径将以不正确的 base-package 前置..所以只需指定完整的包

<context:component-scan base-package="com.abc">
    <!--other filters-->
    <!--.......-->
    <context:exclude-filter expression="com\.abc\.projectA\.service\.orderService\.sectionA\.orderService" type="regex" />
</context:component-scan>

回答by thatidiotguy

When you are setting beans you should be including the full package location of classes so that classes with duplicate names are never a problem. For instance:

当您设置 bean 时,您应该包括类的完整包位置,以便具有重复名称的类永远不会成为问题。例如:

<bean id="orderServiceA" class="com.foo.bar.a.OrderService">

<bean id="orderServiceB" class="com.foo.barr.b.OrderService">

Now you can have both and not run into any issues. Maybe I am missing something, but I do not understand why you need to eliminate a class because it has the same name as another class in a different package. I mean the Java SDK has multiple classes called List and it never causes an issue.

现在您可以同时拥有两者,而不会遇到任何问题。也许我遗漏了一些东西,但我不明白为什么你需要消除一个类,因为它与不同包中的另一个类同名。我的意思是 Java SDK 有多个名为 List 的类,它永远不会导致问题。

回答by dotvav

It seems to me you are trying to break the Unique Package Names convention described in the Java Spec: section 7.7.

在我看来,您试图打破Java Spec: section 7.7 中描述的唯一包名称约定。

Or are you trying to load different versions of the same classes? Your classloader may not be able to disambiguate.

或者您是否正在尝试加载相同类的不同版本?您的类加载器可能无法消除歧义。