Spring Tutorial
From SquadLimberWiki
Dependency Injection
Martin Fowler's article on Inversion of Control aka Dependency Injection
Spring Overview
DAO - Data Access Object
ORM - Object Relational Mapping
AOP - Aspect Oriented Programming
Instantiating a container
Resource resource = new FileSystemResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);
... or...
ClassPathResource resource = new ClassPathResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);
... or...
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
// of course, an ApplicationContext is just a BeanFactory
BeanFactory factory = (BeanFactory) context;
Beans: Importing external bean definitions
<import resource="services.xml"/>
Beans: Instantiating wth default constructor
<bean id="exampleBean" class="examples.ExampleBean"/>
<bean name="anotherExample" class="examples.ExampleBeanTwo"/>
Beans: Instantiating with static factory methods
<bean id="exampleBean"
class="examples.SomeFactory"
factory-method="createInstance"/>
Beans: Instantiating with instance factory methods
<bean id="myFactoryBean" class="..."> ... </bean>
<bean id="exampleBean"
factory-bean="myFactoryBean"
factory-method="createInstance"/>
Using Beans: The BeanFactory interface
boolean containsBean(String): returns true if the BeanFactory contains a bean definition or bean instance that matches the given name
Object getBean(String): returns an instance of the bean registered under the given name. Depending on how the bean was configured by the BeanFactory configuration, either a singleton and thus shared instance or a newly created bean will be returned. A BeansException will be thrown when either the bean could not be found (in which case it'll be a NoSuchBeanDefinitionException), or an exception occurred while instantiating and preparing the bean
Object getBean(String, Class): returns a bean, registered under the given name. The bean returned will be cast to the given Class. If the bean could not be cast, corresponding exceptions will be thrown (BeanNotOfRequiredTypeException). Furthermore, all rules of the getBean(String) method apply (see above)
Class getType(String name): returns the Class of the bean with the given name. If no bean corresponding to the given name could be found, a NoSuchBeanDefinitionException will be thrown
boolean isSingleton(String): determines whether or not the bean definition or bean instance registered under the given name is a singleton (bean scopes such as singleton are explained later). If no bean corresponding to the given name could be found, a NoSuchBeanDefinitionException will be thrown
String[] getAliases(String): Return the aliases for the given bean name, if any were defined in the bean definition
Setter Injection
<bean id="exampleBean" class="examples.ExampleBean"> // setter injection using the nested <ref/> element <property name="beanOne"><ref bean="anotherExampleBean"/></property> // setter injection using the neater 'ref' attribute <property name="beanTwo" ref="yetAnotherBean"/> <property name="integerProperty" value="1"/> </bean> <bean id="anotherExampleBean" class="examples.AnotherBean"/> <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne;
}
public void setBeanTwo(YetAnotherBean beanTwo) {
this.beanTwo = beanTwo;
}
public void setIntegerProperty(int i) {
this.i = i;
}
}
Constructor Injection
<bean id="exampleBean" class="examples.ExampleBean"> // constructor injection using the nested <ref/> element <constructor-arg><ref bean="anotherExampleBean"/></constructor-arg> // constructor injection using the neater 'ref' attribute <constructor-arg ref="yetAnotherBean"/> <constructor-arg type="int" value="1"/> </bean> <bean id="anotherExampleBean" class="examples.AnotherBean"/> <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public ExampleBean(
AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
this.beanOne = anotherBean;
this.beanTwo = yetAnotherBean;
this.i = i;
}
}
Factory method injection
<bean id="exampleBean" class="examples.ExampleBean" factory-method="createInstance"> <constructor-arg ref="anotherExampleBean"/> <constructor-arg ref="yetAnotherBean"/> <constructor-arg value="1"/> </bean> <bean id="anotherExampleBean" class="examples.AnotherBean"/> <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean {
public static ExampleBean createInstance (
AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
ExampleBean eb = new ExampleBean (...);
// some other operations
...
return eb;
}
}
Constructor argument types
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg type="int" value="7500000"/> <constructor-arg type="java.lang.String" value="42"/> </bean>
Constructor argument ordering with index
<bean id="exampleBean" class="examples.ExampleBean"> <constructor-arg index="0" value="7500000"/> <constructor-arg index="1" value="42"/> </bean>
Passing raw values as Constructor / Setter arguments
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> // results in a setDriverClassName(String) call <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mydb"/> <property name="username" value="root"/> <property name="password" value="masterkaoli"/> </bean>
Auto-magic conversion to different types
Spring will often automatically convert injection arguments in to different type e.g. int, boolean etc...
For example, here it will create a Properties object without being told to:
<bean id="mappings" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
// typed as a java.util.Properties !!!
<property name="properties">
<value>
jdbc.driver.className=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb
</value>
</property>
</bean>
It will also convert raw values to beans if there happens to be a bean with the id the same as the string. For example, the following two code snippets are equivalent:
<bean id="theTargetBean" class="..."/>
<bean id="theClientBean" class="...">
<property name="targetName">
<idref bean="theTargetBean" />
</property>
</bean>
<bean id="theTargetBean" class="..."/>
<bean id="client" class="...">
<property name="targetName">
<value>theTargetBean</value>
</property>
</bean>
Beans: Defining collections
<bean id="moreComplexObject" class="example.ComplexObject">
// results in a setAdminEmails(java.util.Properties) call -->
<property name="adminEmails">
<props>
<prop key="administrator">administrator@somecompany.org</prop>
<prop key="support">support@somecompany.org</prop>
<prop key="development">development@somecompany.org</prop>
</props>
</property>
// results in a setSomeList(java.util.List) call -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource" />
</list>
</property>
// results in a setSomeMap(java.util.Map) call
<property name="someMap">
<map>
<entry>
<key>
<value>yup an entry</value>
</key>
<value>just some string</value>
</entry>
<entry>
<key>
<value>yup a ref</value>
</key>
<ref bean="myDataSource" />
</entry>
</map>
</property>
// results in a setSomeSet(java.util.Set) call
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
</bean>


