Spring Core Reference – Cheatsheet


BeanFactory factory  = new XmlBeanFactory(new FileSystemResource(fileName));

ApplicationContext context = new ClassPathXmlApplicationContext(fileName);


By Value
<bean id=”beanName” class=”org.abcd.efg.AClass”>
<property name=”name” value=”Rasesh”/>

By Reference
<bean id=”beanName” class=”org.abcd.efg.AClass”>
<property name=”name” ref=”anotherBeanName”/>


<bean id=”beanName” class=”org.abcd.efg.AClass”>
<constructor-arg index=”0″ value=”Rasesh”/>
<constructor-arg index=”1″ ref=”anotherBeanName”/>


<bean id=”beanName” class=”org.abcd.efg.AClass”>
<property name=”names” >
<ref bean=”beanName”/>
<ref bean=”anotherBeanName”/>

Autowiring by tags

<bean id=”beanName” class=”org.abcd.efg.AClass” autowire=”byName“>

Here, it will search for the beans with the same name as member variables in AClass and autowire them.

<bean id=”beanName” class=”org.abcd.efg.AClass” autowire=”byType“>

Here, it will try and match the data type of beans and member variables in AClass. If there are multiple beans of same data type, this won’t work.

<bean id=”beanName” class=”org.abcd.efg.AClass” autowire=”constructor“>

This is autowire by Type, but this will set the member variables through constructor instead of setters.

Bean Scopes

Singleton, Prototype


Adding Required annotation to setter will fail when it initializes the bean if the value is null instead of a later NPE when it is referenced.

<bean class=”org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor” />

This bean will take care of the process mentioned above.

This works same as auto-wiring by type falling back to auto-wiring by name. If still not found, qualifier can be used.

<bean id=”……>
<qualifier value=”qualifierString”/>


<context:annotation-config/> can be used to avoid adding bean post processor for each annotation.

This along with @Qualifier(“qualifierString”) will be able to auto wire the bean.
<bean class=”org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor” />

This can be used for dependency injection by name. @Resource(name=”beanName”) will wire the bean with specified name.

If name is not specified, it will do auto-wiring by name.

@PostConstruct and @PreDestroy can be used for init method and destroy method with context.registerShutDownHook().


This can be used to avoid xml configuration and spring will create a bean named as the class’s name. This can only handle singleton bean.

<context:component-scan base-package=”org.abcd.efg”/>

@Service, @Controller, @Repository

component-scan tag will pick this up as well. These map to MVC data model.

MessageSource to get text from property files

<bean id=”messageSource” class=”org.springframework.context.support.ResourceBundleMessageSource”>
<property name=”basenames”>

context.getMessage(“greeting”, null, //message parameter
“Default greeting”, null //Locale


MessageSource messageSource = context.getBean(“messageSource”);
messageSource.getMessage(“greeting”, null, //message parameter
“Default greeting”, null //Locale

parameter ex:

drawing.point=Circle: Point is: ({0}, {1})
context.getMessage(“drawing.point”, new Object[] {“val1”, “val2”}, “Default point msg”, null)