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 Video Library
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
View Events Video Library
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
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • Auto Logging in Class and Method Level Using Custom Annotations in Spring Boot App
  • Smart Dependency Injection With Spring: Assignability (Part 2 of 3)
  • Extending Swagger and Spring Doc Open API

Trending

  • Distributed Tracing Best Practices
  • How To Verify Database Connection From a Spring Boot Application
  • Breaking Down Silos: The Importance of Collaboration in Solution Architecture
  • CI/CD Docker: How To Create a CI/CD Pipeline With Jenkins, Containers, and Amazon ECS
  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
25.20K 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.

Related

  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • Auto Logging in Class and Method Level Using Custom Annotations in Spring Boot App
  • Smart Dependency Injection With Spring: Assignability (Part 2 of 3)
  • Extending Swagger and Spring Doc Open API

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

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: