DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Coding
  3. Frameworks
  4. Avoid Spring Annotation Code Smell: Use Spring 3 Custom Annotations

Avoid Spring Annotation Code Smell: Use Spring 3 Custom Annotations

Shekhar Gulati user avatar by
Shekhar Gulati
·
Oct. 27, 10 · Interview
Like (0)
Save
Tweet
Share
24.91K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Annotation Spring Framework Code smell

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Introduction to Cloud Native
  • Leverage Lambdas for Cleaner Code
  • gRPC on the Client Side
  • 5 Recent Amazing AI Advancements

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: