Friday 21 August 2015

Java Source World: Creating spring application in Eclipse IDE

Creating spring application in Eclipse IDE


Creating spring application in Eclipse IDE


Here, we are going to create a simple application of spring framework using eclipse IDE. Let's see the simple steps to create the spring application in Eclipse IDE.

1.    create the java project
2.    add spring jar files
3.    create the class
4.    create the XML file to provide the values
5.    create the test class

Steps to create spring application in Eclipse IDE

Let's see the 5 steps to create the first spring application using eclipse IDE.

1) Create the Java Project

Go to File menu - New - project - Java Project. Write the project name e.g. firstspring - Finish. Now the java project is created.

2) Add spring jar files

There are mainly three jar files required to run this application.

1.    org.springframework.core-3.0.1.RELEASE-A
2.    com.springsource.org.apache.commons.logging-1.1.1
3.    org.springframework.beans-3.0.1.RELEASE-A

For the future use, You can download the required jar files for spring core application.

  1. download the core jar files for spring
  2. download the all jar files for spring including aop, mvc, j2ee, remoting, oxm, etc.

To run this example, you need to load only spring core jar files.

To load the jar files in eclipse IDE, Right click on your project - Build Path - Add external archives - select all the required jar files - finish



3) Create Java class
In such case, we are simply creating the Student class have name property. The name of the student will be provided by the xml file. It is just a simple example not the actual use of spring. We will see the actual use in Dependency Injection chapter. To create the java class, Right click on src - New - class - Write the class name e.g. Student - finish. Write the following code:

    package com.springexample;   
         
    public class Student { 
    private String name; 
     
    public String getName() { 
        return name; 
    } 
          public void setName(String name) { 
        this.name = name; 
    }      
     public void displayInfo(){ 
        System.out.println("Hello: "+name); 
    } 
    } 

This is simple bean class, containing only one property name with its getters and setters method. This class contains one extra method named displayInfo() that prints the student name by the hello message.

4) Create the xml file

To create the xml file click on src - new - file - give the file name such as applicationContext.xml - finish. Open the applicationContext.xml file, and write the following code:

    <?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:p="http://www.springframework.org/schema/p" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
                   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
    <bean id="studentbean" class="com.springexample.Student"> 
    <property name="name" value="Vimal Jaiswal"></property> 
    </bean>
    </beans> 

The bean element is used to define the bean for the given class. The property subelement of bean specifies the property of the Student class named name. The value specified in the property element will be set in the Student class object by the IOC container.

5) Create the test class

Create the java class e.g. Test. Here we are getting the object of Student class from the IOC container using the getBean() method of BeanFactory. Let's see the code of test class.

    package com.springexample; 
     
    import org.springframework.beans.factory.BeanFactory; 
    import org.springframework.beans.factory.xml.XmlBeanFactory; 
    import org.springframework.core.io.ClassPathResource; 
    import org.springframework.core.io.Resource; 
     
    public class Test { 
    public static void main(String[] args) { 
        Resource resource=new ClassPathResource("applicationContext.xml"); 
        BeanFactory factory=new XmlBeanFactory(resource); 
         
        Student student=(Student)factory.getBean("studentbean"); 
        student.displayInfo(); 
    } 
    } 

Now run this class. You will get the output Hello: Vimal Jaiswal.

Read More »

Saturday 15 August 2015

Java Source World: Difference between JDBC and Hibernate

Difference between JDBC and Hibernate | Java Source World

                                                                                                          

                                                                                   Java Source World                   A Java quick reference blog



Difference between JDBC and Hibernate:


1. Hibernate is data base independent, your code will work for all ORACLE,MySQL ,SQLServeretc.In case of JDBC query must be data base specific.

2. As Hibernate is set of Objects , you don't need to learn SQL language.You can treat TABLE as a Object . Only Java knowledge is need.In case of JDBC you need to learn SQL.
3. Don’t need Query tuning in case of Hibernate. If you use Criteria Quires in Hibernate then hibernate automatically tuned your query and return best result with performance.In case of JDBC you need to tune your queries.
4. You will get benefit of Cache. Hibernate support two level of cache. First level and 2nd level. So you can store your data into Cache for better performance.In case of JDBC you need to implement your java cache.
5. Hibernate supports Query cache and It will provide the statistics about your query and database status.
6. Hibernate Supports automatically updating version of rows where JDBC doesn't.
7. Development fast in case of Hibernate because you don't need to write queries
8. No need to create any connection pool in case of Hibernate. In case of JDBC you need to write your own connection pool
9. In the xml file you can see all the relations between tables in case of Hibernate. Easy readability.
Also Read: Hibernate Framework




                                                                                  Java Source World                   A Java quick reference blog

Read More »

Java Source World: Spring Framework - Constructor Injection with Non-String Collection

Constructor Injection with Non-String Collection



Constructor Injection with Non-String Collection (having Dependent Object) Example

  
If we have dependent object in the collection, we can inject these information by using the ref element inside the list, set or map.

In this example, we are taking the example of Forum where One question can have multiple answers. But Answer has its own information such as answerId, answer and postedBy. There are four pages used in this example:
  1.     Question.java
  2.     Answer.java
  3.     applicationContext.xml
  4.     Test.java
In this example, we are using list that can have duplicate elements, you may use set that have only unique elements. But, you need to change list to set in the applicationContext.xml file and List to Set in the Question.java file.
Question.java

This class contains three properties, two constructors and displayInfo() method that prints the information. Here, we are using List to contain the multiple answers.

    package com.springexample; 
     
    import java.util.Iterator; 
    import java.util.List; 
     
    public class Question { 
    private int id; 
    private String name; 
    private List<Answer> answers; 
     
    public Question() {} 
    public Question(int id, String name, List<Answer> answers) { 
        super(); 
        this.id = id; 
        this.name = name; 
        this.answers = answers; 
    } 
     
    public void displayInfo(){ 
        System.out.println(id+" "+name); 
        System.out.println("answers are:"); 
        Iterator<Answer> itr=answers.iterator(); 
        while(itr.hasNext()){ 
            System.out.println(itr.next()); 
        } 
    } 
  } 

Answer.java

This class has three properties id, name and by with constructor and toString() method.

    package com.springexample; 
     
    public class Answer { 
    private int id; 
    private String name; 
    private String by; 
     
    public Answer() {} 
    public Answer(int id, String name, String by) { 
        super(); 
        this.id = id; 
        this.name = name; 
        this.by = by; 
    }        
    public String toString(){ 
        return id+" "+name+" "+by; 
    } 
    } 

applicationContext.xml

The ref element is used to define the reference of another bean. Here, we are using bean attribute of ref element to specify the reference of another bean.

    <?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:p="http://www.springframework.org/schema/p" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
     
    <bean id="ans1" class="com.springexample.Answer"> 
    <constructor-arg value="1"></constructor-arg> 
    <constructor-arg value="Java is a programming language"></constructor-arg> 
    <constructor-arg value="John"></constructor-arg> 
    </bean>       
    <bean id="ans2" class="com.springexample.Answer"> 
    <constructor-arg value="2"></constructor-arg> 
    <constructor-arg value="Java is a Platform"></constructor-arg> 
    <constructor-arg value="Ravi"></constructor-arg> 
    </bean>       
    <bean id="q" class="com.springexample.Question"> 
    <constructor-arg value="111"></constructor-arg> 
    <constructor-arg value="What is java?"></constructor-arg> 
    <constructor-arg> 
    <list> 
    <ref bean="ans1"/> 
    <ref bean="ans2"/> 
    </list> 
    </constructor-arg> 
    </bean> 
     
    </beans> 

Test.java

This class gets the bean from the applicationContext.xml file and calls the displayInfo method.

    package com.springexample; 
     
    import org.springframework.beans.factory.BeanFactory; 
    import org.springframework.beans.factory.xml.XmlBeanFactory; 
    import org.springframework.core.io.ClassPathResource; 
    import org.springframework.core.io.Resource; 
     
    public class Test { 
    public static void main(String[] args) { 
        Resource r=new ClassPathResource("applicationContext.xml"); 
        BeanFactory factory=new XmlBeanFactory(r); 
         
        Question q=(Question)factory.getBean("q"); 
        q.displayInfo(); 
         
    } 
    }  

Read More »

Java Source World: Difference between JAX-RPC, JAX-WS, JAX-RS, Apache Axis, SAAJ, Apache SOAP, JWSDP, Metro, Jersey and GlassFish



Difference between JAX-RPC, JAX-WS, JAX-RS, Apache Axis, SAAJ, Apache SOAP, JWSDP, Metro, Jersey and GlassFish


  • JAX-RPC is a specification/API for Java developers to develop SOAP based interoperable web services. This API is now obsolete, and may be dropped from the next JEE version.
  • JAX-WS is the successor to JAX-RPC. It requires Java 5.0, and is not backwards-compatible to JAX-RPC.  Describes the high-level differences to JAX-RPC.
  • SAAJ is another specification/API for using SOAP envelopes with or without attachments. It operates on a lower level than JAX-RPC or JAX-WS, both of which will use SOAP envelopes based on SAAJ if needed.
  • Apache Axis is an open source implementation of the Java WS APIs for sending and receiving SOAP messages. Axis 1 supports JAX-RPC and SAAJ, while Axis 2 supports SAAJ and JAX-WS.
  • Apache SOAP was the first SOAP implementation. It is now obsolete, and has been superseded by Apache Axis.
  • Sun JWSDP - Sun Java Webservices Developer Pack, is an implementation of JAX-RPC, SAAJ and various other XML Java technologies. It is now deprecated in favor of the Metro stack.
  • GlassFish is the open source JEE reference implementation. As such, it contains implementations of JAX-RS and JAX-WS.
  • Metro is the SOAP stack used in GlassFish?. It supports SAAJ, JAX-WS, WS-Security and other standards.
  • JAX-RS is the standard Java API for RESTful web services.
  • Jersey is the reference implementation of the JAX-RS API, as defined in the JSR-311 standard for RESTful web services.
Read More »