Over a million developers have joined DZone.

When Good Annotations Go Bad

DZone 's Guide to

When Good Annotations Go Bad

· Java Zone ·
Free Resource

At one time not too long ago, I wasn't a big fan of annotations. But then I let my guard down and even started liking them. But now I'm starting to wonder if my dislike was a good thing.

Let's say that I have a class called Document. And let's say that a Document can be indexed into a searchable index using a IndexService class. And let's say that I have a Spring MVC controller class called SearchController that uses IndexService to search for Documents given some search criteria.

Now, just for fun, let's say that IndexService uses Compass to persist Documents to Lucene. And, as for Document, let's say that I chose to use Compass annotations such as @Searchable instead of the Compass' XML mapping.

One final bit of stage-setting: Each of these classes is in a separate project (a separate Maven module). Document is in a "domain" module, IndexService is in an "index" module, and SearchController is in a "web" module.

Okay, so SearchController has a dependency on both IndexService and Document. That's fine, because it will be dealing with both of those directly.

But, as luck would have it, SearchController also transitively depends on @Searchable and any other Compass annotations I use in Document. That means that I can't compile the web module without it having Compass in its build classpath (that is, as a dependency in its POM. Even though SearchController doesn't know about or need to know about Compass!!!

Doesn't this violate the law of demeter?

Someone please tell me that I'm wrong and show me the error in my ways. I want to be wrong here. But I can't seem to be able to build my web module without adding Compass to the list of dependencies...and that seems to be just plain wrong to me.

From http://www.jroller.com/habuma


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}