0

first off I point to the similar question. I spent more than an hour to set this up, but PathMatchingResourcePatternResolver still scans everything.

I have one common.xml (that is imported from specific.xml) and a specific.xml bean definition file. The context is loaded from specific.xml. In common.xml there is this element:

<context:component-scan base-package="cz.instance.transl">
   <context:exclude-filter type="aspectj" 
        expression="cz.instance.transl.model..* &amp;&amp; cz.instance.transl.service..* &amp;&amp; cz.instance.transl.hooks..*"/>   
   </context:component-scan>

Where classes in packages like cz.instance.transl.service.* should not be subject of scanning, but everything else in here cz.instance.transl.* should be scanned through. But PathMatchingResourcePatternResolver marks everything as matching resources. It is the same with regex.

EDITED: If I declare context:component-scan in specific.xml, then the scanning doesn't even start, and I get NoSuchBeanDefinitionException on annotation based dependencies in common.xml.

BTW: in xml style configuration, one can have many components that share a common.xml beans via "import resource" when loading context. How this is done when Annotation-based container configuration is used ?

Community
  • 1
  • 1
lisak
  • 21,611
  • 40
  • 152
  • 243

1 Answers1

2

In this case you need "or" rather than "and":

<context:exclude-filter type="aspectj"
     expression="cz.instance.transl.model..* || cz.instance.transl.service..* || cz.instance.transl.hooks..*"/>       
axtavt
  • 239,438
  • 41
  • 511
  • 482
  • Still scanning everything. This doesn't make sense to me, I need all these three packages excluded. "Exclude either this or this or this is really confusing :-) – lisak Jan 03 '11 at 13:10
  • @lisak: Exclude filter doesn't exclude packages, it exclude classes. It reads: exclude class if it belongs to either this or that package. – axtavt Jan 03 '11 at 13:15
  • There is a problem with the "import resource" cause it seems that context:component-scan applies to the particular xml with bean definition and if there is a dependency in the imported resource that is not declared there, it fails on NoSuchBeanDefinitionException – lisak Jan 03 '11 at 13:15
  • Of course it excludes classes :-) it's just easier to say "exclude packages" than "exclude classes from packages" and imho it makes the same sense – lisak Jan 03 '11 at 13:17
  • @lisak: I don't understand your problem with imported resouces. Is it clear that these filters are applied to the annotated classes discovered by `component-scan`, not to manually declared beans. – axtavt Jan 03 '11 at 13:21
  • @axtavt, then it is correct that PathMatchingResourcePatternResolver is looking for matching resources in the entire directory tree, find all resources, and then loads only those that match ? It's weird because it takes some time and it would make more sense if it searched only through resources specified in the filter – lisak Jan 03 '11 at 13:26
  • @axtavt, although I don't understand what is the difference between logical AND / OR in the aspectj expression... – lisak Jan 03 '11 at 13:37
  • @lisak: There are other kinds of filters, such as `annotation`, that can't be evaluated without looking at the resources. The meaning of `&&` and `||` is exactly the same as for all logical expressions - filter is applied to when both conditions are true, or when either of conditions is true. – axtavt Jan 03 '11 at 15:11