Daily Archives: November 28, 2013

Spring Inversion of Control Containers

Spring Inversion of Control Containers

Spring Container is the most important module in Spring Framework. It manages the files that are included in your application and their objects. These objects are called Java Beans.

Spring Container uses:

– Metadata: a XML configuration file, Java annotations, or Java code

– POJO Classes: Java classes

to make your application.

There are two Spring Containers:

BeanFactory (an org.springframework.beans.factory.BeanFactory interface) – simpler. light-weight applications, mobile, the most important implementation is XmlBeanFactory class

ApplicationContext (an org.springframework.context.ApplicationContextinterface interface) – more complicated, enterprise-oriented, heavy-weight applications, not-mobile, generally recommended to use, the most important implementation are FileSystemXmlApplicationContext, ClassPathXmlApplicationContext, and WebXmlApplicationContext

BeanFactory container is included in ApplicationContext container.

First Application with Spring Framework in Eclipse IDE

First Application with Spring Framework in Eclipse IDE

Using ClassPathXmlApplicationContext implementation of ApplicationContext Container

We prepared our Eclipse IDE for using for creating applications with Spring Framework:

How to Download and Setup Spring Framework for Eclipse IDE

Now, let’s start developing our first program. You should know that Spring technology use Java and XML. They are the most important technologies. So having created our project, create three files in src subdirectory. But Java likes packages, so we have to create one for including our files.

Right click on the src subdirectory -> New -> Package

Name your package. Remember Java convention: inverted domain name of your company. Well, it can be a false company that doesn’t exist in reality. I use

com.linaittech

Lina IT Technologies is my company. So my application’s classes will be on the path:

com.linaittech.class.java

in the Java world.

Now we can create all four files:

/src

MainApp.java

Mirek.java

Lak.java

Beans.xml

Right click on the src subdiirectory -> New -> Class -> name your class: MainApp.java -> Finish

Right click on the src subdiirectory -> New -> Class -> name your class: Mirek.java -> Finish

Right click on the src subdiirectory -> New -> Class -> name your class: Lak.java -> Finish

Right click on the src subdiirectory -> New -> Other -> write xml in Wizards -> choose XML File -> Next -> name your file: Beans.xml -> choose Create XML File from XML template -> Next -> Finish

So we have three Java files and one xml file. The XML file is the configuration file of the whole application. It join the three Java classes into one program. The structure of the program is following:

Beans.xml<—-MainApp.java

————————————————————

Mirek.java                              Lak.java

Thanks to Beans.xml, MainApp.java knows that there are two classes (Mirek.java and Lak.java) to use and what they have inside. It can be more classes in your application. They are POJO (Plain Old Java Objects). We can call all methods in them. That’s why, Beans.xml is the file with metadata. This is managed by Spring container. In this example, we use ClassPathXmlApplicationContext (one of the implementations of ApplicationContext Container). Look at MainApp.java code to see that.

The code for all our files:

Beans.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans xmlns=”http://www.springframework.org/schema/beans&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”&gt;

<bean id=”mirek” class=”com.linaittech.Mirek”>
<property name=”message” value=”Mirek jest gość!”/>
</bean>

<bean id=”lak” class=”com.linaittech.Lak”>
<property name=”num1″ value=”1″/>
<property name=”num2″ value=”2″/>
<property name=”sum” value=”3″/>
</bean>

</beans>

MainApp.java

package com.linaittech;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

    public static void main(String[] args) {
        
        ApplicationContext context = new ClassPathXmlApplicationContext(“Beans.xml”);
        Mirek obj = (Mirek) context.getBean(“mirek”);
        obj.getMessage();
        
        ApplicationContext context1 = new ClassPathXmlApplicationContext(“Beans.xml”);
        Lak obj1 = (Lak) context1.getBean(“lak”);
        obj1.getNum1();
        obj1.getNum2();
        obj1.getSum();
    }

}

Mirek.java

package com.linaittech;

public class Mirek {
    
   private String message;
    
    public void setMessage(String message){
        this.message = message;
        }
    public void getMessage(){
        System.out.println(“Your Important Message : ” + message);
        }

}

Lak.java

package com.linaittech;

public class Lak {
    private int num1;
    private int num2;
    private int sum;
    
    public void getNum1() {
        System.out.println(“The num1 is : ” + num1);
    }
    public void setNum1(int num1) {
        this.num1 = num1;
    }
    public void getNum2() {
        System.out.println(“The num2 is : ” + num2);
    }
    public void setNum2(int num2) {
        this.num2 = num2;
    }
    public void getSum() {
        System.out.println(“The sum is : ” + sum);
    }
    public void setSum(int sum) {
        this.sum = num1 + num2;
    }

}

You don’t have to start a server because so far we aren’t going to display nothing on web. Just run the application in Eclipse: Run -> Run or use Ctrl + F11.

If you run your application in Eclipse IDE, you will get in Console:

lis 28, 2013 12:51:13 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@b1a0f9: startup date [Thu Nov 28 12:51:12 CET 2013]; root of context hierarchy
lis 28, 2013 12:51:13 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
lis 28, 2013 12:51:14 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@10737c8: defining beans [mirek,lak]; root of factory hierarchy
Your Important Message : Mirek jest gość!
lis 28, 2013 12:51:14 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@e3e421: startup date [Thu Nov 28 12:51:14 CET 2013]; root of context hierarchy
lis 28, 2013 12:51:14 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
lis 28, 2013 12:51:15 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@190a6db: defining beans [mirek,lak]; root of factory hierarchy
The num1 is : 1
The num2 is : 2
The sum is : 3
That’s all! Our output is everything what is in red. The rest is just information for you to know  what has happened step by step. You see that there are two outputs from two subclasses (Mirek.java and Lak.java).