How to exclude a class from Spring in applicationContext?

Here we only want to exclude the class from a specific class path, say

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

However, there is another class with the same name, but in a different class path

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

so only filer by class name will not 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> 

This does not work. So, I'm sure that <context:exclude-filter> is only valid at the package level, but not for a specific class? If so, how do you exclude a class from the bean injection so that we can select and select with the class to get a wired connection with the same class name?

Thanks in advance.

+7
source share
3 answers

No, the exception should work, the problem you are facing, you probably assume that the path in the regular expression will be pre-installed with the base package, which is not true .. just specify the full 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> 
+15
source

When you install beans, you must enable the full placement of class packages so classes with duplicate names are never a problem. For example:

 <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 face any problems. Maybe something is missing me, but I don’t understand why you need to exclude the class, because it has the same name as another class in another package. I mean, the Java SDK has several classes called List, and this never causes a problem.

0
source

It seems to me that you are trying to break the convention on unique packages described in Java Spec: section 7.7 .

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

0
source

All Articles