When good annotations go bad - No Fluff Just Stuff

When good annotations go bad

Posted by: Craig Walls on October 21, 2008

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.

Craig Walls

About Craig Walls

Craig Walls is a Principal Engineer, Java Champion, Alexa Champion, and the author of Spring AI in Action, Spring in Action, and Build Talking Apps. He's a zealous promoter of the Spring Framework, speaking frequently at local user groups and conferences and writing about Spring. When he's not slinging code, Craig is planning his next trip to Disney World or Disneyland and spending as much time as he can with his wife, two daughters, 1 bird and 2 dogs.

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »