You are on page 1of 13

Spring 3 Dependency Injection Basics

The Dependency Injection (DI) feature is the core architectural foundation of the Spring
framework. This lab explores various basic dependency injection schemes of Spring framework
Expected duration: 60 minutes

Resources

Chapter 1.1: Dependency Injection and Inversion of Control chapter of Spring


framework reference document

Lab Exercises
Most exercises in this lab are provided in the form of "ready-to-open-and-run" Maven projects in
order to maximize the effectiveness of the learning process. You are welcome to create them from
scratch. It is also strongly encouraged you do as much experimentation of your own by changing
the provided code.

Exercise 0: Import sample maven projects of this lab

Exercise 1: Build and run "di_basics_SimpleValue" maven project (20


minutes)

Exercise 2: Build and run "di_basics_InjectRef" maven project (20 minutes)

Exercise 3: Build and run "di_basics_Naming" maven project (20 minutes)

Prerequisite - Machine Set Up

Install jdk 1.6


Install Springsource Tool Suite

Exercise 0: Import sample maven projects of this lab


1.

Create a new "Java Working Set" called "spring3_di_basics" (or whatever name of
your choice). This is to organize the projects under the working set.

2. Import maven projects of this lab.

Exercise 1: Build and run "di_basics_SimpleValue" maven project


Learning points:

How to set simple values to the properties of a bean using Dependency Injection.

Tasks to be performed
1.

Build and run "di_basics_SimpleValue" project.

2.

Study the project

(1.1) Build and run "di_basics_SimpleValue" maven project


1. Run the application.Right Click->Run->JavaApplication

2. Observe the result in the Console area.


Name:John Smith
Age:35
Age in Seconds: 1103760000
Height: 1.78
Is Programmer?: true

(1.2) Study the project


1. Main.java
package com.softwarecampus.examples;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
private
private
private
private
private

String name;
int age;
float height;
boolean isProgrammer;
Long ageInSeconds;

public static void main(String[] args) {


BeanFactory factory = new ClassPathXmlApplicationContext("/beans.xml");
Main simple = (Main)factory.getBean("injectSimple");
System.out.println(simple);
}

public void setAgeInSeconds(Long ageInSeconds) {


this.ageInSeconds = ageInSeconds;
}
public void setIsProgrammer(boolean isProgrammer) {
this.isProgrammer = isProgrammer;
}
public void setAge(int age) {
this.age = age;
}
public void setHeight(float height) {
this.height = height;
}
public void setName(String name) {
this.name = name;
}

public String toString() {


return "Name:" + name + "\n"
+ "Age:" + age + "\n"
+ "Age in Seconds: " + ageInSeconds + "\n"
+ "Height: " + height + "\n"
+ "Is Programmer?: " + isProgrammer;
}

2. beans.xml
<?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">
<!-- injecting built-in vals sample -->
<bean id="injectSimple" class="com.softwarecampus.examples.Main">
<property name="name">
<value>John Smith</value>
</property>
<property name="age">
<value>35</value>
</property>
<property name="height">
<value>1.78</value>
</property>
<property name="isProgrammer">
<value>true</value>
</property>
<property name="ageInSeconds">
<value>1103760000</value>
</property>
</bean>
</beans>

Exercise 2: Build and run "di_basics_InjectRef" maven project


Learning points:

How to inject an object through ref attribute

Tasks to be performed
1.

Build and run "di_basics_SimpleValue" project.

2.

Study the project

(2.1) Build and run "di_basics_InjectRef" maven project

Aug 27, 2007 8:54:44 AM org.springframework.core.CollectionFactory <clinit>


INFO: JDK 1.4+ collections available
Aug 27, 2007 8:54:44 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader
loadBeanDefinitions
INFO: Loading XML bean definitions from file
[C:\handson2\development\springdi\samples\SpringInjectionInjectRef\beans.xml]
Encyclopedia's are a waste of money - use the Internet

(2.2) Study the project


1. Main.java
package com.softwarecampus.examples;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("/beans.xml");
InjectRef ref = (InjectRef)factory.getBean("injectRef");
System.out.println(ref.getOracle().defineMeaningOfLife());
}
}
2. beans.xml
<?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">
<!-- oracle bean used for a few examples -->
<bean id="oracle" name="wiseworm"
class="com.softwarecampus.examples.BookwormOracle"/>

<!-- injecting reference sample (using id) -->


<bean id="injectRef" class="com.softwarecampus.examples.InjectRef">
<property name="oracle" ref ="oracle" />
</bean>
</beans>
3. InjectRef.java
package com.softwarecampus.examples;
public class InjectRef {
private Oracle oracle;
Oracle getOracle() {
return oracle;
}
public void setOracle(Oracle oracle) {
this.oracle = oracle;
}
}
4. BookwormOracle.java
public class BookwormOracle implements Oracle {
private Encyclopedia enc;
public void setEncyclopedia(Encyclopedia enc) {
this.enc = enc;
}
public String defineMeaningOfLife() {
return "Encyclopedia's are a waste of money - use the Internet";
}
}
5. Oracle.java
public interface Oracle {
}

public String defineMeaningOfLife();

Exercise 3: Build and run "di_basics_Naming" maven project


Learning points:

You can use various naming schemes for refering beans

Tasks to be performed

1.

Build and run "di_basics_Naming" project.

2.

Study the project

(3.1) Build and run "di_basics_Naming" maven project

true
true
true
aliases = name2 name3

(3.2) Study the project


1. Main.java
package com.softwarecampus.examples;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
BeanFactory factory = new ClassPathXmlApplicationContext("/beans.xml");
String
String
String
String

s1
s2
s3
s4

=
=
=
=

(String)factory.getBean("name1");
(String)factory.getBean("name2");
(String)factory.getBean("name3");
(String)factory.getBean("name4");

System.out.println((s1 == s2));
System.out.println((s2 == s3));
System.out.println((s3 == s4));
String[] x = factory.getAliases("name3");
System.out.println("aliases = " + x[0] + " " + x[1]);
}

2. beans.xml
<?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">
<!-- naming examples -->
<bean id="string1" class="java.lang.String"/>
<bean name="string2" class="java.lang.String"/>
<bean class="java.lang.String"/>
<!-- aliasing examples -->
<bean id="name1" name="name2,name3,name4" class="java.lang.String"/>

</beans>

You might also like