Spring | Struts |
Spring is an application framework in which Spring MVC is one of the modules of Spring framework. | Struts is a web framework which can be used to develop web applications. |
Spring implements IOC Design Pattern. | Struts implement MVC Design Pattern. |
It provides abstraction layer on multiple Java/J2EE technologies including Servlet, JSPs. Also provides abstraction on other framework software. | It provides abstraction layer only on Servlet, JSP technology. |
Spring is a Layered Architecture. | Struts is a not a Layered Architecture. |
Spring Framework is said to be a non-invasive means it doesn’t force a programmer to extend or implement their class from any predefined class or interface given by Spring API. | Struts is said to be invasive. In Struts we used to extend Action Class. It forces the programmer that, the programmer class must extend from the base class provided by Struts API. |
Spring provides easy integration with ORM, JDBC technologies. | In Struts, integrating with ORM, JDBC technology, we need to do manually coding. |
Gives built-in middleware services like transaction, logging, connection pooling etc. | Doesn’t give built-in middleware services. |
Spring is Lightweight framework. | Struts is Heavyweight framework. |
Spring is loosely coupled. | Struts is tightly coupled. |
It is not easy to integrate with other client side technologies. | It is easy to integrate with other client side technologies. |
Spring MVC just provides tags for basic HTML form tags. | Struts 2 provides many out-of-box JSF styled complex and composite tags Such as: Ajax DOJO tags |
For View component, Spring allows JSP, Velocity, Freemarker, Excel and PDF | For View component Struts allows only JSP. |
Spring MVC provides more handler mappings. | Struts doesn’t have specific handler mappings but uses Action mappings. |
Spring clear module division between Model, View and Controller. | Struts mixes Controller and Model. |
Advantages of Spring Framework:
| Advantages of Struts Framework:
|
Sunday, February 2, 2014
Difference between Spring and Struts framework:
The major differences between Spring framework and Struts framework are :
Saturday, February 1, 2014
Migrating to Spring Framework 4.0
Hi friends to migrate from 3.x to 4.x the documentation is available in git hub. Click here to see.
or
Git Hub URL: https://github.com/spring-projects/spring-framework/wiki/Migrating-from-earlier-versions-of-the-spring-framework
or
Git Hub URL: https://github.com/spring-projects/spring-framework/wiki/Migrating-from-earlier-versions-of-the-spring-framework
Friday, January 24, 2014
Tuesday, January 14, 2014
Externalization in Java
In my previous post ( Serialization), we saw about Serialization. While using Serialization we can get some limitations.
Limitations of Serialization:
- File size is very high because it contains meta information of the file.
- Customization due to transient which is not effective because we get “null” in place of transient attributes.
Externalization(java.io.Externalizable):
Externalization is nothing but serialization but by implementing Externalizable interface to persist and restore the object. To make our object as externalize we need to implement with Externalizable interface.
Unlike Serializable interface, it is not a marker interface which contains two methods
public void writeExternal(ObjectOutput out) throws IOException;
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;
In Serialization process, JVM has full control for serializing object but in Externalization process application/developer gets control for persisting objects. In Externalization process, writeExternal() and readExternal() method provides complete control on format and content of Serialization process.
In writeExternal() method we are making attributes as externalized by using corresponding methods for different types of data.
writeInt() --> for Integer
writeLong() --> for Long
writeDouble() --> for Double
writeUTF() --> for String (UTF --> Universal Text Format)
writeObject() --> for Derived attributes other than String & Wrapper classes etc..
In the same way, readExternal() method also provides some methods. By using those methods we can get the externilized attributes.
readInt();
readLong();
readDouble();
readUTF();
readObject();
Employee.java
package com.ranga;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
/**
* Created by ranga on 1/12/14.
*/
public class Employee implements Externalizable {
private long serialVersionUID = 31462223600l;
private long id;
private String firstName;
private String lastName;
private int age;
private String address;
public Employee() {
super();
}
public Employee(long id, String firstName, String lastName, int age, String address) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.address = address;
}
public Employee(int age) {
this.age = age;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeLong(id);
out.writeUTF(firstName);
out.writeUTF(lastName);
out.writeInt(age);
out.writeUTF(address);
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
id = in.readLong();
firstName = in.readUTF();
lastName = in.readUTF();
age = in.readInt();
address = in.readUTF();
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
ExternalizationDemo.java
package com.ranga;
import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
/**
* Created by ranga on 1/13/14.
*/
public class ExternalizationDemo {
public static void main(String[] args) {
Employee employee = new Employee();
employee.setId(143);
employee.setFirstName("Ranga");
employee.setLastName("Reddy");
employee.setAge(25);
employee.setAddress("MPL, Chittoor, AP");
FileOutputStream fileOutputStream = null;
ObjectOutputStream objectOutputStream = null;
try {
fileOutputStream = new FileOutputStream( new File("employees.ser"));
objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(employee);
System.out.println("Employee Object Externalization Done Successfully.");
} catch(Exception ex) {
ex.printStackTrace();
} finally {
if(objectOutputStream != null) {
try {
objectOutputStream.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
if(fileOutputStream != null) {
try {
fileOutputStream.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
}
}
Output:
Employee Object Externalization Done Successfully.
DeExternalizationDemo.java
package com.ranga;
import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
/**
* Created by ranga on 1/13/14.
*/
public class DeExternalizationDemo {
public static void main(String[] args) {
FileInputStream fileInputStream = null;
ObjectInputStream objectInputStream = null;
try {
fileInputStream = new FileInputStream(new File("employees.ser"));
objectInputStream = new ObjectInputStream(fileInputStream);
Employee employee = (Employee) objectInputStream.readObject();
System.out.println("Employee Info: "+employee);
} catch(Exception ex) {
ex.printStackTrace();
} finally {
if(objectInputStream != null) {
try {
objectInputStream.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
if(fileInputStream != null) {
try {
fileInputStream.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
}
}
Output:
Employee Info: Employee{id=143, firstName='Ranga', lastName='Reddy', age=25, address='MPL, Chittoor, AP'}
Only those fields will be persisted as byte stream that will be saved by writeExternal and readExternal. Other field in the class will not be externalized. So by Externalizable interface java developer has complete control on java class persistence.
Externalizable provides complete control, it also presents challenges to serialize super type state and take care of default values in case of transient variable and static variables in Java. If used correctly Externalizable interface can improve performance of serialization process.
Advantages:
The main advantages of externalization over serialization are:
1)File size is highly reduced(nearly 1/3)
2) Customization is very easy and more effective.
Performance issue:
1. Further more if you are subclassing your externalizable class you will want to invoke your superclass’s implementation. So this causes overhead while you subclass your externalizable class.
2. methods in externalizable interface are public. So any malicious program can invoke which results into lossing the prior serialized state.
Difference between Serialization and Externalization:
1. With the classes which implement
Serializable, the serialization of the object is taken care of automatically, while classes that implement Externalizable is responsible for serializing itself, without the help of default serialization procedures. 2. when we serialize an Externalizable object, a default constructor will be called automatically after that will the readExternal() method be called.
Sunday, January 12, 2014
How to get the System Properties and System Environment variables by using Java
- To get the System properties we can use getProperty() of System class.
- To get all the System properties we can use the getProperties() of System class.
package com.ranga;
/**
* Created by ranga on 1/12/14.
*/
public class Application {
public static void main(String[] args) {
System.out.println("Java Version: "+ System.getProperty("java.version"));
}
}
Output:
Java Version: 1.6.0_35
Fetching all System properties.
---------------------------------------------------------------
package com.ranga;
import java.util.Map;
import java.util.Properties;
/**
* Created by ranga on 1/12/14.
*/
public class Application1 {
public static void main(String[] args) {
Properties properties = System.getProperties();
for(Object key : properties.keySet()) {
System.out.println("Key: "+ key +", Value: "+properties.getProperty(key.toString()));
}
}
}
Output:
Key: java.runtime.name, Value: Java(TM) SE Runtime Environment
Key: sun.boot.library.path, Value: /usr/java/jdk1.6.0_35/jre/lib/amd64
Key: java.vm.version, Value: 20.10-b01
Key: java.vm.vendor, Value: Sun Microsystems Inc.
Key: java.vendor.url, Value: http://java.sun.com/
Key: path.separator, Value: :
Key: idea.launcher.port, Value: 7554
Key: java.vm.name, Value: Java HotSpot(TM) 64-Bit Server VM
Key: file.encoding.pkg, Value: sun.io
Key: sun.java.launcher, Value: SUN_STANDARD
Key: user.country, Value: US
Key: sun.os.patch.level, Value: unknown
Key: java.vm.specification.name, Value: Java Virtual Machine Specification
Key: user.dir, Value: /home/ranga/install/ITappCloud/SerializationExample
Key: java.runtime.version, Value: 1.6.0_35-b10
Key: java.awt.graphicsenv, Value: sun.awt.X11GraphicsEnvironment
Key: java.endorsed.dirs, Value: /usr/java/jdk1.6.0_35/jre/lib/endorsed
Key: os.arch, Value: amd64
Key: java.io.tmpdir, Value: /tmp
Key: line.separator, Value:
Key: java.vm.specification.vendor, Value: Sun Microsystems Inc.
Key: os.name, Value: Linux
Key: sun.jnu.encoding, Value: UTF-8
Key: java.library.path, Value: /usr/java/jdk1.6.0_35/jre/lib/amd64/server:/usr/java/jdk1.6.0_35/jre/lib/amd64:/usr/java/jdk1.6.0_35/jre/../lib/amd64:/home/ranga/Softwares/idea-IU-133.193/bin::/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Key: java.specification.name, Value: Java Platform API Specification
Key: java.class.version, Value: 50.0
Key: sun.management.compiler, Value: HotSpot 64-Bit Tiered Compilers
Key: os.version, Value: 3.9.10-100.fc17.x86_64
Key: user.home, Value: /home/ranga
Key: user.timezone, Value:
Key: java.awt.printerjob, Value: sun.print.PSPrinterJob
Key: idea.launcher.bin.path, Value: /home/ranga/Softwares/idea-IU-133.193/bin
Key: file.encoding, Value: UTF-8
Key: java.specification.version, Value: 1.6
Key: java.class.path, Value: /usr/java/jdk1.6.0_35/jre/lib/plugin.jar:/usr/java/jdk1.6.0_35/jre/lib/rt.jar:/usr/java/jdk1.6.0_35/jre/lib/deploy.jar:/usr/java/jdk1.6.0_35/jre/lib/management-agent.jar:/usr/java/jdk1.6.0_35/jre/lib/charsets.jar:/usr/java/jdk1.6.0_35/jre/lib/resources.jar:/usr/java/jdk1.6.0_35/jre/lib/jce.jar:/usr/java/jdk1.6.0_35/jre/lib/javaws.jar:/usr/java/jdk1.6.0_35/jre/lib/jsse.jar:/usr/java/jdk1.6.0_35/jre/lib/ext/sunjce_provider.jar:/usr/java/jdk1.6.0_35/jre/lib/ext/sunpkcs11.jar:/usr/java/jdk1.6.0_35/jre/lib/ext/localedata.jar:/usr/java/jdk1.6.0_35/jre/lib/ext/dnsns.jar:/home/ranga/install/ITappCloud/SerializationExample/target/classes:/home/ranga/Softwares/idea-IU-133.193/lib/idea_rt.jar
Key: user.name, Value: ranga
Key: java.vm.specification.version, Value: 1.0
Key: sun.java.command, Value: com.intellij.rt.execution.application.AppMain com.ranga.MyTest2
Key: java.home, Value: /usr/java/jdk1.6.0_35/jre
Key: sun.arch.data.model, Value: 64
Key: user.language, Value: en
Key: java.specification.vendor, Value: Sun Microsystems Inc.
Key: java.vm.info, Value: mixed mode
Key: java.version, Value: 1.6.0_35
Key: java.ext.dirs, Value: /usr/java/jdk1.6.0_35/jre/lib/ext:/usr/java/packages/lib/ext
Key: sun.boot.class.path, Value: /usr/java/jdk1.6.0_35/jre/lib/resources.jar:/usr/java/jdk1.6.0_35/jre/lib/rt.jar:/usr/java/jdk1.6.0_35/jre/lib/sunrsasign.jar:/usr/java/jdk1.6.0_35/jre/lib/jsse.jar:/usr/java/jdk1.6.0_35/jre/lib/jce.jar:/usr/java/jdk1.6.0_35/jre/lib/charsets.jar:/usr/java/jdk1.6.0_35/jre/lib/modules/jdk.boot.jar:/usr/java/jdk1.6.0_35/jre/classes
Key: java.vendor, Value: Sun Microsystems Inc.
Key: file.separator, Value: /
Key: java.vendor.url.bug, Value: http://java.sun.com/cgi-bin/bugreport.cgi
Key: sun.io.unicode.encoding, Value: UnicodeLittle
Key: sun.cpu.endian, Value: little
Key: sun.desktop, Value: gnome
Key: sun.cpu.isalist, Value:
--------------------------------------------------------------------------------------------------------------
To get System environment variables we can use the System.getenv() method.
package com.ranga;
import java.util.Map;
/**
* Created by ranga on 1/12/14.
*/
public class Application2 {
public static void main(String[] args) {
Map<String, String> env = System.getenv();
for(String key : env.keySet()) {
System.out.println(key +" --> "+env.get(key));
}
}
}
The main difference between System.getProperty() and System.getenv() is
System.getProperty() is for JVM arguments which are passed -DpropName=value to java application launcher(java) and System.getenv() is for Operating System environment variables.
