Spring Book – Chapter 4 – Configuration Styles

Stereotype annotations

Annotations can greatly reduce the amount of XML bean configuration and wiring needed. Stereotype annotations are markers for any class that fulfills a role within an application. Stereotypes defined in Spring today include Component, Repository, Service, Controller and Configuration. Spring also defines these stereotypes as specializations of a more generic stereotype, Component. The Component annotation allows creating new stereotypes in future versions of the framework. It also allows you to define your own stereotype components. Spring actually defines five stereotype annotations. The below table lists the four stereotypes in the Spring Framework:

Stereotype Annotation Description
@Component Generic stereotype
@Repository A class that serves in the persistence layer of the application as a data access object
@Service A class that is a business service façade, often providing some reusable business processing
@Controller A controller component in the presentation layer of the application, as it relates to a MVC-designed application
@Configuration Allows the developers to configure the beans in the Java class itself. Spring annotation is a special class to be used by the Spring IoC to configure beans

To have the Spring Framework auto detect the stereotype beans, include a component-scan element in the Spring XML configuration file. The component scanning functionality of Spring will be detailed in the following sections of this chapter in detail.

The scan element forces the Spring container to search the package specified in the base-package attribute for all stereotype components. The base-package attribute specifies the parent package to look for stereotype components, but the Spring container scans all sub-packages as well. Use a comma-separated list of package names when you need to search multiple packages:

By default, all components marked with the @Repository, @Service, @Controller, and @Component annotations are detected in the scan. However we can customize this behavior which will be explained in detail later on.

The other Spring Projects like Spring Web Services, Spring Integration etc. have their own stereotype annotations. The basic of defining a stereotype annotation remains the same as explained in this section.

Creating your own stereotype annotations

If you would like to have your own stereotype annotation, create a new annotation of your choice and then annotate the newly created annotation with @Component. Spring’s component scanning will automatically detect your new annotation and register it to the Spring’s container. Example is shown below for your reference:

Meta-annotations

Annotations which can annotate other annotations are called meta-annotations. In the above section (Creating your own stereotype annotations), I indirectly gave you explanation of what a meta-annotation is. Consider the same example as above, in this @Component is applied to your customer annotation namely MyStereotypeAnnotation. Here @Component is used to annotate your custom @MyStereotypeAnnotation annotation. Here, @Component is called as meta-annotation. All the stereotype annotation explained above can be used as meta-annotation with any issues. Component scan declared will automatically look into these and do the appropriate actions necessary. Spring’s @Scope and @Transactional annotations can be applied as meta-annotations on custom annotations now. This is a powerful way of defining custom stereotype annotations with extended default semantics.

Java-based configuration

Spring 3.0 introduced java based configuration. Main annotation which replaces the actual XML file is called @Configuration. Each bean element in XML can be done by annotating method with @Bean inside a class annotated with @Configuration. You now have a choice of using only Java code to configure your beans in your application. Being Java, you do have full control over the bean instantiation and some developers feel this way of configuring beans to be more easy and understandable. By doing the configuration of beans in java, it’s considered to be more type-safe compared to XML.

Lifecycle

Figure 4-6. Java based configuration lifecycle

Spring container scans the classes in an application having @Configuration and reads all the method signatures having @Bean annotation. Once this is done the BeanFactoryPostProcessor’s defined in the application are called. Soon after this, the actual bean instantiation and dependency injection happens in which methods in the class annotated with @Configuration having @Bean annotation methods implementations are called. If BeanPostProcessor’s are defined, these are called soon after this. Enabling @Configuration class processing can be achieved in the following ways:

  • Using component scanning

Complete elimination of XML doesn’t happen in this way. You need to at least have this declaration in the application-config.xml.

  • Using AnnotationConfigApplicationContext

Complete elimination of XML. Here your configuration class file namely YourAppConfig should be annotated with @Configuration.

Listing. @Configuration Example

In the above java configuration example the methods which are annotated with @Bean can have any access modifier except private. The method name, namely “loyaltyService” and “customerRepository” becomes the bean id. The bean in the java configuration is by default singleton. If you want a different scope, you can annotate the method with @Scope providing appropriate scope as required. For Spring container to load these configuration files, it should have a default constructor either explicitly declared or automatically given by the Java compiler. Similar to XML configuration, multiple Java configuration classes can exist and the Spring container loads all these configuration classes as required according to your requirement.

Spring container loads the classes having @Configuration annotation and creates a proxy around it in which the actual magic of Spring happens like creation of singletons, ApplicationContext storing etc.

Page Visitors: 4529

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

2 thoughts on “Spring Book – Chapter 4 – Configuration Styles

  1. Hi there,

    There is one mistake repeated several times here. The value of the propertyInteger is being set using “ref” instead of “value” and you may want to remove the spaces before and after some of the values/properties to avoid others new to the framework copying and pasting and getting tripped up.

    Apart from that, thanks for making the material available for free.

    1. Sure will do the necessary changes. I wrote the material using MS Word and after that when i copied, these spaces automatically came in.

      Thanks for pointing it out.

      Regards
      Tomcy John

Leave a Reply

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