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:
1 2 3 4 5 6 7 8 9 |
@Documented @Scope @Retention(value=RUNTIME) public @interface Singleton{ } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<beans> <bean> <qualifier value="sampleOne"/> </bean> <bean> <qualifier value="sampleTwo"/> </bean> </beans> |
Listing. Java Class using Spring @Qualifier to distinguish type-injected object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class LoyaltyServiceImpl { @Autowired @Qualifier("sampleOne") Sample sampleOne; @Autowired @Qualifier("sampleTwo") Sample sampleTwo; } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class MyController { private String dirLocation; // Use the image.dirLocation property value found in the settings array @Value("#{settings[‘image.dirLocation’]}") public void setDirLocation(String val) { this.dirLocation = val; } } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public class Customer { private String firstName; //…. @Required public void setFirstName(String firstName) { this.firstName = firstName; } //…. } |
Listing. XML configration required to make the Spring container aware of @Required annotation
1 2 3 4 5 |
<!—- One way --> <context:annotation-config /> <!-- Another way --> <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> |
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
1 2 3 4 5 |
<beans default-lazy-init="true"> <!-- no beans will be pre-instantiated... --> </beans> <bean id="lazyBean" class="com.mybook.service.LoyaltyServiceImpl" lazy-init="false"/> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
@Configuration public class MyJavaConfig { @Bean public MyBean myBean() { return new MyBean(); } } |
Page Visitors: 4349

Tomcy John

Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
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.
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