Spring Annotations Cheat Sheet for Daily Usage
In Java world, Spring Framework/Boot is a must-have skill set. XML-Based Configuration was for a long time main mean. But overloaded xmls with unreadable structure has become quickly headache for developers. Spring has started to switch from XML-based configuration to Annotation-based Configuration. Nowadays, greenfield projects are structured almost always in Annotation-based manner. Based on my experience and with some research I have tried to summarize the most important Spring annotations that a developer needs in a daily usage.
Context…
These annotations are there for injecting and creating beans for a specific spring context:
- @Autowired — used for injecting a bean in a runtime. It can be used with method, variable or constructor (preferred).
- @Configuration — class level annotation that declares one or more @Bean methods to be injected by Spring container.
- @Scope — used for declaring scope of the bean. For Instance: singleton or prototype etc.
- @Qualifier — used for the cases if there are more than one implementation of the interface, where spring does not know which bean to inject(throws NoUniqueBeanDefinitionException). Together with @Autowired this annotation eliminates ambiguous situations.