Important Annotations for Spring Core Tasks

This is a quick summary of important annotations in Spring for doing core tasks in Spring. Some of them might have already been covered in examples. Some of them might be covered later. Some of them might be a variation of the already covered ones, so please feel free to try them out and let us know if you face any issues.

 

@Configuration, @Bean

You can use @Configuration to specify a Java configuration file. 

A Java configuration file can be used instead of XML configuration for configuring your application.

Spring looks for methods annotated with the @Bean annotation in a class with the @Configuration annotation. 

@Bean is used to specify a bean instance definition.

Example: http://javajee.com/your-first-spring-program-using-java-configuration.

 

@Component, @Repository, @Service, @Controller and @ComponentScan

@Component can be used over a class to tell spring to consider it as a bean component. 

@ComponentScan annotation can be used to specify the packages that need to be scanned for components and is usually used along with @Configuration classes. You may also specify the same behaviour using <context:component-scan in spring xml configuration.

@Repository, @Service and @Controller can be used instead of @Component based on actual application layer you are developing. However technically all behave the same. If in doubt, use @Component.

Example (Component and ComponentScan): http://javajee.com/your-first-spring-program-using-component-and-compone....

Example (Controller and ComponentScan): http://javajee.com/factory-beans-in-spring-using-annotations-example

 

@Autowired, @Resource, @Inject, @Qualifier, @Primary

You can simply specify the @Autowired annotation over properties, setter methods and constructors. Spring will then find a bean of matching type and inject it. By default, it does autowiring by type and hence if you have two matching beans (two beans of same type), your execution will fail.

@Qualifier annotation can be used along with @Autowired to do autowiring by name. You can specify the name of the bean as @Qualifier("beanName"). 

@Primary allows to give preference to a bean when multiple beans match for autowiring.

If you are using Java configuration with @Bean annotations, you can specify the name in your @Bean annotation as @Bean(name="beanName")

@Resource and @Inject are Java specific annotations that does almost the same thing as Spring's @Autowired. However, when using @Resource, you can specify autowiring by name instead of using @Qualifier annotation.

Example (Autowired): http://javajee.com/factory-beans-in-spring-using-annotations-example

 

@Profile, @ActiveProfiles

@Profile allows to specify a profile for your bean.

Example: http://javajee.com/your-first-spring-profiles-based-program

@ActiveProfiles can be used from a JUnit test to specify the active profiles.

Example: http://javajee.com/junit-testing-of-spring-code-with-profiles-example

 

@Lazy

@Lazy allows your bean to be lazily loaded.

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!