Shekhar Gulati is a Java Consultant with over 5 years experience.He is currently working with Xebia India an Agile Software Development company.The postings on this site and on his blog are his own and do not necessarily represent opinion of his employer. His own blog is at http://whyjava.wordpress.com/ and you can follow him on twitter here. Shekhar has posted 25 posts at DZone. View Full User Profile

Avoid Spring Annotation Code Smell: Use Spring 3 Custom Annotations

10.27.2010
| 15815 views |
  • submit to reddit

With Spring 3, annotations are becoming the defacto standard for defining bean configuration. Annotations like @Configuration, @Bean allow you to create externalized configuration definitions in Java. It is equivalent to the configuration we define in XML. But as we use annotations, we start duplicating these annotations in different classes. For example, our UserService will have following declaration

@Service
@Scope(value = "prototype")
@Transactional(readOnly = true, rollbackFor = RuntimeException.class)
public class UserService {

}

In this bean definition we have configured UserService as a service component with prototype scope, with readonly transactions which will get roll-backed for RuntimeException. As our application evolves, we will have other classes with the same set of annotations  like

@Service
@Scope(value = "prototype")
@Transactional(readOnly = true, rollbackFor = RuntimeException.class)
public class RoleService {

}

This is a sort of code smell, as we are duplicating the same set of annotations in different classes. If, in future, we decide to change the transaction strategy i.e. making readonly equal to false, then we have to make changes in all the classes (supposing you need the same configuration in all classes). This type of problem can be solved by defining your own custom annotations. Custom annotations have different annotations applied to them. So, in our application we can have our own service annotation like

@Service
@Scope(value = "prototype")
@Transactional(readOnly = true, rollbackFor = RuntimeException.class)
public @interface ReadOnlyService{

}

The best part of using custom annotations is that you don't have to make any configuration, Spring will auto detect that these beans are service components and everything will work fine. Custom Annotations are a very small feature added in Spring but are very useful. Next time you see your annotations code smell think about using custom annotations.

Published at DZone with permission of its author, Shekhar Gulati.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Adedayo Abiodun replied on Wed, 2010/10/27 - 9:42am

Nice!! Wasn't aware of this.. Spring 3 seems an enticing release

Behrang Saeedzadeh replied on Wed, 2010/10/27 - 7:19pm

Nice, but I would have named the annotation ReadOnlyService.

Shekhar Gulati replied on Thu, 2010/10/28 - 12:20am in response to: Behrang Saeedzadeh

makes sense. I would change it. Thanks

Cliff Meyers replied on Sat, 2010/10/30 - 8:36pm

I believe you'll need to set the RetentionPolicy to your annotation to RUNTIME for this to work, depending on your configuration.

Khent Johnson replied on Fri, 2011/09/02 - 2:06pm

Great! This is so brilliant idea! Anyway, I like the term you used the "code smell". You clearly explained well the subject matter which touches the annotations. And the samples you added from your explanations makes the topic more easy to understand. Nicely done! GAR Labs

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.