Spring Book – Chapter 4 – Configuration Styles

JSR-330 @Singleton

The @Singleton annotation identifies a type that the injector only instantiates once. Spring’s equivalent to this annotation is @Scope(“singleton”). This annotation’s specification is as follows:

Spring and JSR-330 @Qualifier

In the case of type-driven injection (DI in which object type is used to inject the actual bean), Spring container performs search based on the object type when searching for the relevant bean. If several beans are found of the same type, you can use @Qualifier for more detailed control to decide the target bean.

Listing. XML Configuration which shows type-injection without specifying bean id

Listing. Java Class using Spring @Qualifier to distinguish type-injected object

Spring @Value

To inject values into your classes Spring’s @Value annotation can be used. Following is an example of injecting a property (loaded from property files into the array settings) into the method parameter:

Listing. Usage of Spring’s @Value annotation

Spring @Required

The @Required when written on top of setFirstName() method in class Customer makes sure that firstName property must be set otherwise it will give error message and states that bean creation fails.

Simply applying the @Required annotation will not enforce the property checking, you will have to either register RequiredAnnotationBeanPostProcessor or include <context:annotation-config /> in the bean configuration file.

Listing. Java class in which @Required annotation is used

Listing. XML configration required to make the Spring container aware of @Required annotation

Spring @Lazy

If you want to initialize your beans lazily Spring provides @Lazy annotation, which you can use in your java configuration class.

If you prefer XML configuration and would like to load your beans lazily, you can do the following as well:

Listing. XML configuration for making beans loaded lazily in Spring container

Spring @Configuration

The @Configuration Spring annotation configures as special class to be used by the Spring IoC to configure beans. The @Bean annotation instructs the IoC container to execute the function and do the necessary inside the function. By default the name of the method is used as the bean name. This is pure-java approach to configure the Spring IoC Container.

Page Visitors: 3641

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 *