Today we will share a simple Spring 3 inversion of control hello world example program.
What is Inversion of Control (IOC) or Dependency Injection (DI)?
Inversion of control or dependency injection is an object oriented programming technique, in which the object coupling is done at runtime instead of compile time. The responsibility of creating, injecting and managing the life cycle of the object is inverted from programmer to framework.
Types of Dependency Injection.(DI)
- Constructor Injection.
- Setter Injection.
- Interface Injection.
For more details on inversion control and related patterns please refer to this post from Martin Fowler.
Java based IOC containers.
Name of container | Interface injection | Constructor injection | Setter injection |
---|---|---|---|
Spring | No | Yes | Yes |
Pico | No | Yes | Yes |
Guice | ? | Yes | Yes |
XWork | Yes | ? | ? |
WebWork | Yes | ? | ? |
In the above listed containers Spring is the most commonly used DI framework. Google Guice is still in beta version.
Spring 3 IOC Hello World example program.
In this example we are using spring 3.2.3.RELEASE.
Maven repository for Spring 3.
1 2 3 4 5 6 7 |
<repository> <id>com.springsource.repository.maven.snapshot</id> <url>http://maven.springframework.org</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> |
Spring 3.2.3 Dependencies.
1 2 3 4 5 6 7 8 9 10 11 |
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>3.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>3.2.3.RELEASE</version> </dependency> |
Spring 3 IOC example complete code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
package com.ourownjava.spring.ioc; /** * Spring 3 IOC Example Program. * * @author ourownjava.com * */ public class HelloWorldBean { private User user; public User getUser() { return user; } public void setUser(final User user) { this.user = user; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.ourownjava.spring.ioc; /** * Spring 3 IOC Example Program. * * @author ourownjava.com * */ public class User { private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(final String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(final String lastName) { this.lastName = lastName; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<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.2.xsd"> <!-- value setter injection --> <bean id="user" class="com.ourownjava.spring.ioc.User"> <property name="firstName" value="Sanju"/> <property name="lastName" value="Thomas"/> </bean> <!-- reference setter injection --> <bean id="helloWorldBean" class="com.ourownjava.spring.ioc.HelloWorldBean"> <property name="user" ref="user"/> </bean> </beans> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package com.ourownjava.spring.ioc; import static org.junit.Assert.assertEquals; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Spring 3 IOC Example Program. * * @author ourownjava.com * */ public class HelloWorldBeanBehavior { private static ApplicationContext context; @BeforeClass public static void setUp(){ context = new ClassPathXmlApplicationContext("SpringBeans.xml"); } @Test public void shouldFindUserBeanByName(){ final User user = (User) context.getBean("user"); assertEquals("Thomas", user.getLastName()); assertEquals("Sanju", user.getFirstName()); } @Test public void shouldFindHelloWorldBeanByName(){ final HelloWorldBean helloWorldBean = (HelloWorldBean)context .getBean("helloWorldBean"); assertEquals("Thomas", helloWorldBean.getUser().getLastName()); assertEquals("Sanju", helloWorldBean.getUser().getFirstName()); } } |