Skip to content

Instantly share code, notes, and snippets.

@ProfAvery
Last active October 11, 2015 01:17
Show Gist options
  • Save ProfAvery/3779686 to your computer and use it in GitHub Desktop.
Save ProfAvery/3779686 to your computer and use it in GitHub Desktop.
Spring Configuration Quick Reference
XML NAMESPACES
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!--Bean declarations go here-->
</beans>
<bean id="name" class="package.classname" />
APPLICATION CONTEXT
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
MyObject o = (MyObject) ctx.getBean("name");
PROPERTY INJECTION
<bean id="name" class="package.classname">
<property name="property" value="value" />
<property name="property" ref="id" />
<property name="property">
<bean class="package.classname" />
</property>
</bean>
CONSTRUCTOR INJECTION
<bean id="name" class="package.classname">
<constructor-arg value="value" />
<constructor-arg ref="id" />
</bean>
FACTORY METHODS
<bean id="name" class="package.classname" factory-method="method" />
SCOPING
Make getBean() return a new instance each time:
<bean id="name" class="package.classname" scope="prototype" />
(default is "singleton")
SETUP AND TEARDOWN
<bean id="name" class="package.classname"
init-method="init" destroy-method="destroy" />
COLLECTION INJECTION
<bean id="name" class="package.classname">
<property name="collection">
<list>
<ref bean="element" />
<ref bean="element" />
<ref bean="element" />
</list>
</property>
</bean>
<set> works the same
<map>
<entry key="name" value=ref="element" />
<entry key="name" value=ref="element" />
<entry key="name" value=ref="element" />
</map>
<props>
<prop key="name">value</prop>
<prop key="name">value</prop>
<prop key="name">value</prop>
</props>
INJECTING NULL
<property name="property"><null /> </property>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment