Spring Annotation Tutorial

@Component :       

It is generic  stereotype for any spring managed component. While other stereotypes like @Repository, @Controller, @Service are specialized form of @Component for specific use. Specilized form of stereotypes(e.g. @Controller) should take precedence over @Component as it will have extra benifit of pointcuts, better suited for processing by spring tools. Since Spring 2.5.

@Repository :

Specialized form of @Component. This annotation work as marker for any class which fulfills the role of repository or Data Access  Object(Persistent Layer). Since Spring 2.0

@Controller :

Specialized form of @Component and applied to Presentation Layer.

@Service :

Specialized form of @Component and applied to Service Layer.

@Configuration :

@Configuration annotated class are same like old XML file where we used to define beans. @Configuration is configuring the beans using Java classes. Each @Configuration annotated class is a configuration in its own. @Configuration can be used in conjuction with @Bean to create beans. Java class annotated with @Configuration will have methods to instantiated and configure the dependencies. Such methods will be annotated with @Bean.  @Configuration annotated classes consist principally of @Bean-annotated methods that define instantiation, configuration, and initialization logic for objects that will be managed by the Spring IoC container.

@Bean

It is a method level annotation which work with @Configuration as mentioned above. It is same like <bean> element in XML configuration. It can also be used in Component Class(Annotated with @Configuration).

Above configuration is same as

@Scope

Every dean defined in IoC container needs to have a scope, default scope is Singleton. @Scope is used in order to specify the scope of Beans defined with @Bean annotation. StandardScopes class is there to define all 4 type of scope.

@DependsOn

@Primary

@Lazy

@Import

@ImportResource

@Value

To get this work you need to add property file in application context XML.

2 Comments Spring Annotation Tutorial

Leave A Comment

Your email address will not be published. Required fields are marked *