Monday, 31 March 2014

Accessing resources from Spring Application context

The following example illustrates accessing resources (both external to the project and internal) from the spring application context.

To learn this, we need to first learn about the org.springframework.core.io.Resource interface. This interface contains some methods which you can learn from the above url. All those methods are are self-explanatory. One method that I would like to pin point is the createRelative() method.
This method takes in a string, the relative path of the current resource and creates a resource object for it. An example, will give you a better understand. For example, consider a resource 'r' pointing to the E:/java/sample.txt. Now, when i call r.createRelative("ding/asdf.txt"); then I will get a Resource object pointing to E:/java/ding/asdf.txt

Create a project in eclipse

So, I'll be naming the project as spring1 since this is the first demo. Note that I haven't used STS (Spring Tool Suite) which most will probably be using. Rather, I have just downloaded the required jar files we need to create our project and included them in the library. Therefore, we will be carrying on creating a simple eclipse project and adding the necessary spring jars. I am using Eclipse KEPLER.

  1. File -> New -> Project -> Java Project
  2. Give the project name spring28 and click Finish 
  3. Now, the project is created.
  4. Under the Project Explorer (in the sidebar) you will see spring1. If you aren't able to see Project Explorer, go to Window menu -> Show view -> Project Explorer.
  5. Right click on the spring28 and click Properties
  6. On the left side, click on Java Build Path.
  7. Select the Libraries tab and then click on Add External Jars
  8. Now, add these jar files starting with: spring-beans, spring-context, spring-core, commons-logging
  9. Click on OK and then you are ready.

Project structure


SpringPrg.java - Main class



package spring28;



import java.io.InputStream;



import org.springframework.context.support.GenericXmlApplicationContext;

import org.springframework.core.io.Resource;



public class SpringPrg {



    @SuppressWarnings("resource")

    public static void main(String args[])

    {

        GenericXmlApplicationContext gc=new GenericXmlApplicationContext();

       

        Resource r1=gc.getResource("classpath:resources/sample.txt");

        Resource r2=gc.getResource("file:///e:/java/ascii.java");

       

        displayInfo(r1);

        displayInfo(r2);

    }

   

    public static void displayInfo(Resource r)

    {

        System.out.println("The file name is "+r.getFilename());

        System.out.println("The description is "+r.getDescription());

       

        try

        {

        System.out.println("The contents of the file is \n"+getContentAsString(r.getInputStream()));

        }catch(Exception e){}

        System.out.println("-------------------------------------");

    }

   

    public static String getContentAsString(InputStream is) throws Exception

    {

    StringBuffer sb=new StringBuffer();

    int k;

   

        while((k=is.read())!=-1)

        {

            sb.append((char)k);

        }

       

    return sb.toString();

    }

   

}

Friday, 28 March 2014

Using inherit attribute in Spring bean tag

The following example illustrates using inherit attribute in the spring <bean> tag.




1. Create a project in eclipse

  1. File -> New -> Project -> Java Project
  2. Give the project name spring3 and click Finish 
  3. Now, the project is created.
  4. Under the Project Explorer (in the sidebar) you will see spring3. If you aren't able to see Project Explorer, go to Window menu -> Show view -> Project Explorer.
  5. Right click on the spring3 and click Properties
  6. On the left side, click on Java Build Path.
  7. Select the Libraries tab and then click on Add External Jars
  8. Now, add these jar files starting with: spring-beans, spring-context, spring-core, spring-expression, commons-logging
  9. Click on OK and then you are ready.
Now right click on the src folder under spring3 and New -> Class and name it SpringPrg

Main program - SpringPrg.java



package spring3;

import org.springframework.context.support.GenericXmlApplicationContext;

public class SpringPrg {

    @SuppressWarnings("resource")
    public static void main(String args[])
    {
        GenericXmlApplicationContext gc=new GenericXmlApplicationContext();
        gc.load("classpath:applicationContext.xml");
        gc.refresh();
       
        Student st=gc.getBean("st2",Student.class);
       
        System.out.println(st.getSno()+" "+st.getSname()+" "+st.getAge());
    }
   
}

The code here is normal. I have used the GenericXmlApplicationContext class which loads the given xml file and reads the bean definitions i.e. it reads how to create bean class objects from the xml file.

The gc.getBean() method returns the object st2 defined in the XML file. The xml file is given below.

Now, again in the package explorer, right click on spring3 and then New -> Class and name it Student

Bean class - Student.java



package spring3;

public class Student {

private int sno;
private String sname;
private int age;

    public void setSno(int sno)
    {
        this.sno=sno;
    }
   
    public int getSno()
    {
        return sno;
    }
   
    public void setSname(String sname)
    {
        this.sname=sname;
    }
   
    public String getSname()
    {
        return sname;
    }
   
    public void setAge(int age)
    {
        this.age=age;
    }
   
    public int getAge()
    {
        return age;
    }
   
}

This is a very simple bean class that takes three general properties of a Student sno, sname and age. Values to these properties are set in the xml file.

Now right click on the src in package explorer, and New -> Other and type xml file in the textbox, hit enter and give it the name applicationContext.xml.

Bean definitions - applicationContext.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">
       
        <bean id="st1" class="spring3.Student">
            <property name="sno" value="101"/>
            <property name="sname" value="Rama"/>
            <property name="age" value="20"/>
        </bean>
       
        <bean id="st2" class="spring3.Student" parent="st1">
            <property name="age" value="22"/>
        </bean>
       
</beans>

As you can see, first we have created a bean object called st1 for the class spring3.Student and then in it we have set the properties, sno to 101, sname to Rama, age to 20. That code can be interpreted into Java like this..



Student st1=new Student();


st1.setSno(101);


st1.setSname("Rama");


st1.setAge(20);

Now, we have also created another object for Student class called st2. You can notice the parent attribute which points to st1. This means that the properties of st1 will be copied to st2.
Additionally, we have included a property tag in st2 setting age to 22. This line, tells that it overrides the age property copied from the st1. In Java the code can be interpreted as..



Student st2=new Student();


st2.setSno(st1.getSno());


st2.setSname(st1.getSname());


st2.setAge(st1.getAge());


st2.setAge(22);


abstract attribute

In the SpringPrg.java program, you can also get the st1 object like this..

Student st=gc.getBean("st1",Student.class);
System.out.println(st1.getSno()+" "+st1.getSname()+" "+st1.getAge());

But if you don't want st1 to be accessed i.e. looked up or don't want to create an object for it, then you should place the abstract="true" while defining the st1. Replace the line..



<bean id="st1" class="spring3.Student">

with the following..



<bean id="st1" class="spring3.Student" abstract="true">

Tuesday, 25 March 2014

Using @Autowired and @Component - Spring Dependency Injection

In this example we will discuss spring dependency injection using annotations. Here I'll introduce you to two simple annotations, @Component and @Autowired. In the previous example, we have declared those beans in the application context xml file. But in this file, we will create only one bean and we will inject that bean into another class via annotations.

Simple bean - Message.java


package spring2;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component("msgObject")
public class Message {

private String message;

    @Autowired
    public void setMessage(String message)
    {
        this.message=message;
    }
   
    public String getMessage()
    {
        return message;
    }
}

The @Autowired annotation allows the container to automatically inject the message property from the application-context.xml file. The container searches for String bean object in the xml file and injects it here. If multiple String beans exist, then we will get an error.

The @Component tells that Message is a component, a bean whose object has to be created. The "msgObject" is the name of the created object.

Application context - application-context.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"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd">
       
        <context:annotation-config/>
        <context:component-scan base-package="spring2"/>
       
        <bean id="myMsg" class="java.lang.String">
            <constructor-arg type="java.lang.String" value="Rama"/>
        </bean>
</beans>


Here we have created only one bean that too for java.lang.String. As you can the name of the object is myMsg and we are using the String(String) constructor to create the object. In Java code, the snippet will be


String myMsg=new String("Rama");


The first line, <context:annotation-config/> searches for annotations i.e. it searches for where the dependencies has to be injected.
The second line, <context:component-scan/> searches for dependencies (those values which has to be injected). This takes base-package attribute which will be the package where the dependencies are scanned.

Dependencies are nothing but objects that are essential to create/use other objects.

http://www.springframework.org/schema/context/spring-context-3.0.xsd contains definitions for annotation-config and component-scan tags.

Namespace is a name given to a set of tags. These tags are defined in XSD files (xml schema definition files). In our application-context.xml we will be using tags from different xsd files and sometimes multiple xsd files might contain tags with the same name. So in order to avoid the ambiguous situation, a namespace is provided. Here, context is the namespace. We call it tag prefix here, telling the container that we are using the tag that is defined in the spring-context-3.0.xsd file.

Main class - SpringPrg1.java



package spring2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringPrg1 {
    public static void main(String args[])
    {
        ApplicationContext ctx=new ClassPathXmlApplicationContext("spring2/application-context.xml");
        Message msg=ctx.getBean("msgObject",Message.class);
        System.out.println(msg.getMessage());
    }
}

Here, we will get the msgObject which is not defined in the XML file, as you can see. Instead, it is written using annotation in the Message class itself.

Here, we have used ctx.getBean("msgObject",Message.class) instead of ctx.getBean("msgObject") in order to avoid explicit type-casting.

Spring Core Hello World example

Here is a very simple spring core hello world example that will guide you to learning the spring in an easy way. The following example will contain two simple classes and an xml file which makes it easy for you to understand the very large concepts of spring.

1. Download Spring framework


Many of the developers use maven, a tool that will automatically download the necessary jar files for a project. But in this tutorial, I'll not be using maven instead, the theme of the tutorial is to teach spring and not maven with spring, I suggest manually downloading the jar files so that you'll get a better idea of what jar files you require for your project. However, at real time it is recommended to use a tool like the maven.

http://repo.spring.io/release/org/springframework/spring/

In the above link, you will be provided with several versions of spring. Download the latest version possible. Download those that end with -dist.zip because these files contain the jar files that we need for our project.
After downloading extract them to a location. You will also need commons-logging because spring uses it for logging purposes.

http://commons.apache.org/proper/commons-logging/download_logging.cgi

Till now, in this blog, we have seen writing programs in Notepad and then compiling them and executing them in the command prompt. While this is a good practice for starters, I recommend using Eclipse (the most popular IDE for Java developers) to continue with the Spring because a developer, in real time environment will be using an IDE to write programs. Hope you will embrace this change.

2. Create a project in eclipse

So, I'll be naming the project as spring1 since this is the first demo. Note that I haven't used STS (Spring Tool Suite) which most will probably be using. Rather, I have just downloaded the required jar files we need to create our project and included them in the library. Therefore, we will be carrying on creating a simple eclipse project and adding the necessary spring jars. I am using Eclipse JUNO.

  1. File -> New -> Project -> Java Project
  2. Give the project name spring1 and click Finish 
  3. Now, the project is created.
  4. Under the Project Explorer (in the sidebar) you will see spring1. If you aren't able to see Project Explorer, go to Window menu -> Show view -> Project Explorer.
  5. Right click on the spring1 and click Properties
  6. On the left side, click on Java Build Path.
  7. Select the Libraries tab and then click on Add External Jars
  8. Now, add these jar files starting with: spring-beans, spring-context, spring-core, spring-expression, commons-logging
  9. Click on OK and then you are ready.
 

Simple bean - Message.java

A bean is nothing more than a class that represents a particular entity. This class will contain details about that entity which can be accessed using its object.



package spring1;



public class Message {



private String message;



    public void setMessage(String message)

    {

        this.message=message;

    }

   

    public String getMessage()

    {

        return message;

    }

}

The class contains setMessage() which is used to set the message and getMessage() which is used to get the message.

Main class - SpringPrg.java

This class will contain the main method and here we will be getting the object of Message class from the factory.



package spring1;



import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;



public class SpringPrg {

    public static void main(String[] args)

    {

        ApplicationContext ctx=new ClassPathXmlApplicationContext("spring1/context.xml");

        Message m=(Message)ctx.getBean("message");

        System.out.println(m.getMessage());

    }

}

Here ApplicationContext is an interface that is implemented by ClassPathXmlApplicationContext class. This class will take an XML file which will contain how bean objects are created. According to this, the spring container creates the bean objects and gets them for us.

The advantage of getting a bean object from the container is that, the container reads the xml file and creates the object according to the procedure mentioned in that file. So, if you would like to change the way you create the object, for example, if you want to use a different constructor, then you need not change the code of your Java program, you'll only need to change that in the xml file. Therefore, you will remove the burden of recompiling the class file every time you want to change the message or use a different constructor.
Also, sometimes you might need to create a lot of objects for use in different classes. So at that time, you'll probably write the creation code like this.


Message m=new Message();

m.setMessage("Hello world!");

Two lines of code, every time you want to use it in a class. It will be a problem, especially if you have 100+ classes that will use this object and therefore you will need to write 200 lines of code. In some cases, object creation might take 5 lines or more , so you'll need to write 500 extra lines of code. For example,


A a=new A();

B b=new B();

C c=new C();

D d=new D();

X x=new X(a,b,c,d);

Application Context - context.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">

    <!-- Create object for spring1.Message with name message -->
    <bean id="message" class="spring1.Message">
   
        <!-- Call message.setMessage("Hello world!") -->
        <property name="message" value="Hello world!"/>
   
    </bean>

</beans>

This is an XML file which you might have undergone several times. The <beans> tag contains several bean definitions i.e. how those bean object has to be created. A bean here is a class.

xmlns: This stands for xml namespace. When you declare xmlns="" means that it is the default namespace. Here the only namespace we are using is beans.
xsi:schemaLocation : Specifies the location of the schema. The http://www.springframework.org/schema/beans/spring-beans-3.0.xsd file contains definitions for the <beans> and <bean> tag and so on.

You can give this file any name you want but make sure that the same name is passed in the ClassPathXmlApplicationContext.