Second Application with Spring Framework in Eclipse IDE

Second Application with Spring Framework in Eclipse IDE

Using FileSystemXmlApplicationContext 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 FileSystemXmlApplicationContext (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”>
<property name=”message” value=”Mirek jest gość!”/>
</bean>

<bean id=”lak”>
<property name=”num1″ value=”1″/>
<property name=”num2″ value=”2″/>
<property name=”sum” value=”3″/>
</bean>

</beans>

MainApp.java

package com.linaittech1;

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

public class MainApp {

    public static void main(String[] args) {
        
        ApplicationContext context = new FileSystemXmlApplicationContext (“file:/home/poganin/worksSpringer/Spring2/src/Beans.xml”);
        Mirek obj = (Mirek) context.getBean(“mirek”);
        obj.getMessage();
        
        ApplicationContext context1 = new FileSystemXmlApplicationContext (“file:/home/poganin/worksSpringer/Spring2/src/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 29, 2013 1:59:55 PM org.springframework.context.support.FileSystemXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@1b24628: startup date [Fri Nov 29 13:59:55 CET 2013]; root of context hierarchy
lis 29, 2013 1:59:55 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from URL [file:/home/poganin/worksSpringer/Spring2/src/Beans.xml]
lis 29, 2013 1:59:56 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@50abcc: defining beans [mirek,lak]; root of factory hierarchy
Your Important Message : Mirek jest gość!
lis 29, 2013 1:59:56 PM org.springframework.context.support.FileSystemXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@774f6e: startup date [Fri Nov 29 13:59:56 CET 2013]; root of context hierarchy
lis 29, 2013 1:59:56 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from URL [file:/home/poganin/worksSpringer/Spring2/src/Beans.xml]
lis 29, 2013 1:59:56 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@eed1b8: 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).

You see that the out is the same as in

First Application with Spring Framework in Eclipse IDE

Using ClassPathXmlApplicationContext implementation of ApplicationContext Container

two subclasses (Mirek.java, Lak.java, and Beans.xml) are the same in both cases, but MainApp.java is different.

Warning:

Be careful. Eclipse IDE has many bugs that can lead you astray. When you working with FileSystemXmlApplicationContext, be sure that you’ve fixed “missing libraries” error before running your application. You should check before every code changing. Go to details at:

JAR Libraries Missing in Order and Export Tab in Eclipse IDE

JAR Libraries Missing in Order and Export Tab in Eclipse IDE

If not, the running of your project can fail in spite of good code without errors.

Leave a comment