Tuesday, January 12, 2010

Interface injection using Spring

I am adding one more sample for Interface Injection using spring. Spring directly does not support interface injection but there is one thing called autowire, that can be used in spring to achieve some sort of interface injection.

Consider below sample Java classes:


1. Text.java - interface which has getter and setter for String object

public interface Text {
void setTextString(String textString);
String getTextString();
}

2. MyText.java - implements above Text interface

public class MyText implements Text {

private String textString;

public String getTextString() {
return textString;
}

public void setTextString(String textString) {
this.textString = textString;
}

}

3. Service.java - which has getter and setter for above Text object

public interface Service {
void setText(Text text);
Text getText();
}

4. MyService.java - implements above Service Interface

public class MyService implements Service {
private Text text;

public Text getText() {
return text;
}

public void setText(Text text) {
this.text = text;
}
}

5. MyServiceApp.java - which has main method to test interface injection

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import static java.lang.System.out;


public class MyServiceApp {

public static void main(String...strings) {
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
"myservice.xml"));
MyService myService = (MyService) beanFactory.getBean("myService");
out.println( myService.getText().getTextString());
}
}


Now, in above sample to get interface injection major file is configuration file(myservice.xml).

Look at below configuration


Configuration file: myservice.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">


<bean id="text" class="MyText" p:textString="My First String...!"/>

<!-- <bean id="myService" class="MyService" p:text-ref="text"/> -->
<!-- The above can be used if you want to inject the dependency yourself-->

<!-- The below can be used to leave the dependency injection decision to the IoC container -->
<!-- <bean id="myService" class="MyService" autowire="byName"/> -->

<!-- The following line is also almost same as the above except for this case IoC will
detect dependency by type -->
<bean id="myService" class="MyService" autowire="byType"/>

</beans>


In above sample you can configure interface injection by two ways:

1. <bean id="myService" class="MyService" p:text-ref="text"/>

Here, injection is done by yourself by giving reference to another bean object.

2. <bean id="myService" class="MyService" autowire="byName"/>

Here, injection decision is taken by IoC container as you have specified autowire attribute. autowire value can be set as byName or byType.

Thursday, January 7, 2010

Constructor Injection using Spring

I wrote about Spring & Setter Injection in my previous blog now I am adding one more sample using constructor Injection. Constructor injection is almost same like setter injection except only difference that here IoC container initializes the constructor parameters.

Consider below sample java class.


Java Class: TestConstructor.java

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

public class TestConstructor {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"constructorinjection.xml"});
Inject inject = (Inject) context.getBean( "myInject" );
System.out.println( inject.getName() );
}
}

class Inject {

private String name;

public Inject(String name, String text)
{
this.name = name + text;
}

public String getName() {
return this.name;
}
}


In above class constructor parameter values(name & text) would be injected from constructorinjection.xml configuration file.


Configuration file: constructorinjection.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:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<bean id="myInject" class="Inject">
<constructor-arg type="java.lang.String" index="1">
<value>Welcome to example to constructor injection </value>
</constructor-arg>
<constructor-arg type="java.lang.String" index="0">
<value>A text </value>
</constructor-arg>
</bean>
</beans>


Here, </constructor-arg> has two optional attributes type and index. Type is not significant in this particular example but can be useful when using some other complex types. Specify type value based on your constructor type. Index is also one useful mechanism to be used within. Based on index value it assigns value to constructor parameter.

In above sampe output is:
A text Welcome to example to constructor injection

Now if you change index vaue 1 to 0 and 0 to 1 output is:
Welcome to example to constructor injection A text

If you have noticed I made one more change in this sample compare to my previous blog. I used ApplicationContext instead of BeanFactory. What is the difference? We will now see why ApplicationContext. The first thing you can notice is that you can (of course optionally) pass more than one XML bean definition/configuration files. Ultimately we are assigning the ApplicatioContetx instance to BeanFactory.

Wednesday, January 6, 2010

Use CodeModel to generate Java Source Code

CodeModel is a library that allows you to generate Java source code in a type-safe fashion.

When to use CodeModel:

If you have huge chunk of data in terms of text file or XML and want to generate some Java class based on those data you can use CodeModel. It also helps when your data changes frequently.

To know more about the CodeModel visit following link:

http://fisheye5.atlassian.com/browse/~raw,r=1.601/jaxb-architecture-document/www/doc/com/sun/codemodel/package-summary.html

Giving you small sample how to use CodeModel to generate your own class.



Example: CodeFactory.java

import com.sun.codemodel.JAnnotationUse;
import com.sun.codemodel.JBlock;
import com.sun.codemodel.JClass;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JDocComment;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JPackage;
import com.sun.codemodel.JType;
import com.sun.codemodel.JVar;

/**
*
* @author naman
*/
public class CodeFactory {

// Method to get JType based on any String Value
public JType getTypeDetailsForCodeModel(JCodeModel jCodeModel, String type) {
if (type.equals("Unsigned32")) {
return jCodeModel.LONG;
} else if (type.equals("Unsigned64")) {
return jCodeModel.LONG;
} else if (type.equals("Integer32")) {
return jCodeModel.INT;
} else if (type.equals("Integer64")) {
return jCodeModel.LONG;
} else if (type.equals("Enumerated")) {
return jCodeModel.INT;
} else if (type.equals("Float32")) {
return jCodeModel.FLOAT;
} else if (type.equals("Float64")) {
return jCodeModel.DOUBLE;
} else {
return null;
}
}

// Function to generate CodeModel Class
public void writeCodeModel(String factroyPackage) {
try {

/* Creating java code model classes */
JCodeModel jCodeModel = new JCodeModel();

/* Adding packages here */
JPackage jp = jCodeModel._package(factroyPackage);

/* Giving Class Name to Generate */
JDefinedClass jc = jp._class("GeneratedFactory");

/* Adding annotation for the Class */
jc.annotate(com.myannotation.AnyXYZ.class);

/* Adding class level coment */
JDocComment jDocComment = jc.javadoc();
jDocComment.add("Class Level Java Docs");


/* Adding method to the Class which is public static and returns com.somclass.AnyXYZ.class */
String mehtodName = "myFirstMehtod";
JMethod jmCreate = jc.method(JMod.PUBLIC | JMod.STATIC, com.somclass.AnyXYZ.class, "create" + mehtodName);

/* Addign java doc for method */
jmCreate.javadoc().add("Method Level Java Docs");

/* Adding method body */
JBlock jBlock = jmCreate.body();

/* Defining method parameter */
JType jt = getTypeDetailsForCodeModel(jCodeModel, "Unsigned32");
if (jt != null) {
jmCreate.param(jt, "data");
} else {
jmCreate.param(java.lang.String.class, "data");
}

/* Defining some class Variable in mthod body */
JClass jClassavpImpl = jCodeModel.ref(com.somclass.AnyXYZ.class);
jvarAvpImpl = jBlock.decl(jClassavpImpl, "varName");
jvarAvpImpl.init(JExpr._new(jClassavpImpl));


/* Adding some direct statement */
jBlock.directStatement("varName.setCode(100);");

/* returning varibalbe */
jBlock._return(jvarAvpImpl);

/* Building class at given location */
jCodeModel.build(new File("generated/src"));

} catch (JAXBException ex) {
logger.log(Level.SEVERE, "JAXBException:" + ex);
ex.printStackTrace();
} catch (Exception ex) {
logger.log(Level.SEVERE, "Other Exception which in not catched:" + ex);
ex.printStackTrace();
}
}

// Wirte main mehtod and call writeCodeModel("com.test") function to generate class
}


After running above class it generates GeneratedFactory class under generated/src/com/test folder. It includes all required imports and also format the class as per Java Standard. It generates as described below.


Generated Class: GeneratedFactory.java

package com.test;

import com.myannotation.AnyXYZ;
import com.somclass.AnyXYZ;

/**
* Class Level Java Docs
*
*/
@com.myannotation.AnyXYZ
public class GeneratedFactory {

/**
* Method Level Java Docs
*
*/
public static com.somclass.AnyXYZ myFirstMehtod(long data) {
com.somclass.AnyXYZ varName = new com.somclass.AnyXYZ();
varName.setCode(100);
return varName;
}
}

Tuesday, January 5, 2010

Java Design Patterns

You can go through below link to get more idea on Different design patterns. You must have to try sample also there.

Link: http://www.javacamp.org/designPattern/

I will add more details on the same later on.

Monday, January 4, 2010

How to use Java Persistence API (JPA)?

The Java Persistence API, sometimes referred to as JPA, is a Java programming language framework that allows developers to manage relational data in applications. A persistence entity is a lightweight Java class whose state is typically persisted to a table in a relational database. Instances of such an entity correspond to individual rows in the table. Entities typically have relationships with other entities, and these relationships are expressed through object/relational metadata. Object/relational metadata can be specified directly in the entity class file by using annotations, or in a separate XML descriptor file distributed with the application.

Let's start with Simple Java Code.

Consider following table in any rational database.


Table Structure:

CREATE TABLE `persistencemsg` (
`messageCode` INT(10) NOT NULL,
`sessionId` VARCHAR(500) NULL,
`message` BLOB(4294967295),
PRIMARY KEY (`messageCode`),
INDEX `messageCode` (`messageCode`)
)
ENGINE = INNODB;


Now, we will write corresponding entity class (JPIPersistenceMessage.java) for the same. @Table annotation represents table name of the table, in our case it is 'PERSISTENCEMSG'. This entity class is always implements Serializable to write thread safe code. It contains getter and setter method for each column in the table. @Column annotation represents column name corresponding to the each column in the table. Go through below Java code.


Class: JPIPersistenceMessage.java

import java.io.Serializable;
import java.nio.ByteBuffer;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

/**
*
* @author naman
*/
@Entity
@Table(name = "PERSISTENCEMSG")
public class JPIPersistenceMessage implements Serializable {

private byte [] message;
private int messageCode;
private String sessionId;

/**
* @return the message
*/
@Column(name = "MESSAGE")
public byte[] getMessage() {
return message;
}

/**
* @param message the message to set
*/
public void setMessage(byte[] message) {
this.message = message;
}

/**
* @return the messageCode
*/
@Column(name = "MESSAGECODE")
public int getMessageCode() {
return messageCode;
}

/**
* @param messageCode the messageCode to set
*/
public void setMessageCode(int messageCode) {
this.messageCode = messageCode;
}

/**
* @return the sessionId
*/
@Column(name = "SESSIONID")
public String getSessionId() {
return sessionId;
}

/**
* @param sessionId the sessionId to set
*/
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
}


Now, how do we use this class for add/edit/update/find operation? First, we need to connect to the database so we have to write one XML file called 'persistence.xml' which is part of META-INF folder in your application. It contains properties to connect to the database. Please change as per your requirement.


XML File: persistence.xml

<persistence
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="persist_name">
<class>JPIPersistenceMessage</class>
<properties>
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="toplink.jdbc.password" value="password"/>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/databasename"/>
<property name="toplink.jdbc.user" value="username"/>
<property name="toplink.jdbc.read-connections.max" value="3"/>
<property name="toplink.jdbc.read-connections.min" value="1"/>
<property name="toplink.jdbc.read-connections.shared" value="true"/>
<property name="toplink.jdbc.write-connections.max" value="5"/>
<property name="toplink.jdbc.write-connections.min" value="2"/>
<property name="toplink.cache.type.default" value="Full"/>
<property name="toplink.target-database" value="MySQL4"/>
</properties>
</persistence-unit>
</persistence>


Let's write manager class (JPIPersistenceManager.java) for database operations. We need to load EntityManagerFactory using persistence-unit name given in persistence.xml file, in this case it is 'persist_name'. Now load EntityManager based on EntityManagerFactory. Now you can call all database operations like add/delete/find on this EntityManager object. User must have to start and close the transaction during each operation. Please go through below code.


Class: JPIPersistenceManager.java

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
*
* @author naman
*/
public class JPIPersistenceManager {

private static EntityManagerFactory emf;
private static EntityManager em;

public void createEntityManagerFactory(String persistenceUnitName) {
emf = Persistence.createEntityManagerFactory(persistenceUnitName);
}

private static void createEntityManager() {
em = emf.createEntityManager();
}

private static void closeEntityManager() {
em.close();
}

private static void createTransactionalEntityManager() {
createEntityManager();
em.getTransaction().begin();
}

private static void closeTransactionalEntityManager() {
em.getTransaction().commit();
closeEntityManager();
}

public void storeMessage(JPIPersistenceMessage persistenceMessage) {

createTransactionalEntityManager();
em.persist(persistenceMessage);
closeTransactionalEntityManager();
}

public void deleteMessage(long messageCode) {

JPIPersistenceMessage persistenceMessageDelete = findMessage(messageCode);
createTransactionalEntityManager();
em.remove(em.merge(persistenceMessageDelete));
closeTransactionalEntityManager();
}

public JPIPersistenceMessage findMessage(long messageCode) {

createTransactionalEntityManager();
JPIPersistenceMessage jpipm = em.find(JPIPersistenceMessage.class, Long.toString(messageCode));
closeTransactionalEntityManager();
return jpipm;

}

public JPIPersistenceMessage updateMessage(byte[] message, long messageCode) {

createTransactionalEntityManager();
Query query = em.createQuery(
"UPDATE JPIPersistenceMessage p SET p.message = :message where p.messageCode = :messageCode");
query.setParameter("message", message);
query.executeUpdate();
closeTransactionalEntityManager();
return findMessage(new Long(messageCode));
}

public static void main(String args[]) {
JPIPersistenceManager persistenceManager = new JPIPersistenceManager();
persistenceManager.createEntityManagerFactory("persist_name");

// Now call storeMessage,deleteMessage, updateMessage and findMessage on persistenceManager object.
}
}


Hope it should be useful to you. Please let me know any comment on this...

Tuesday, December 29, 2009

Simple Spring Application

Spring could be (read intended to be) the one-stop-shop for enterprise application development but at the same time it is light-weight, non-intrusive and modular. Spring allows you to use a particular part/module of spring without bothering about the rest of the spring.


Different types of IoC (Inversion of Control):
Central to the Spring Framework is its Inversion of Control container, which provides a consistent means of configuring and managing Java objects using callbacks. The container is responsible for managing object life cycles: creating objects, calling initialization methods, and configuring objects by wiring them together.
  1. Setter Injection: The examples below are based on setter injection. IoC sets the value of the type parameters of setter methods with the help of bean configuration XML file.
  2. Interface Injection: Spring directly does not support interface injection but there is one thing called autowire, that can be used in spring to achieve some sort of interface injection.
  3. Constructor Injection: constructor injection is almost same like setter injection except only difference that here IoC container initializes the constructor parameters.

Download latest spring framework from http://www.springframework.org

Let's start with sample application using Setter Injection:


First write simple java file call Main.java. It has one subclass call Inject.java which contains some getter and setter methods to set properties. Main class contains main method which loads XML file and set the properties for the Inject class. This code uses spring to load properties from XML file.

The advantage is no need to recompile code again to set different properties. Every time it loads data from XML file. So any change needed just change XML file content.


Code Snapshot:

1. Main.java 

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Main {

public static void main(String[] args) {
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
"context.xml"));
Inject demo = (Inject) beanFactory.getBean("mybean");
System.out.println(demo);
}
}

class Inject {

private String name;
private int age;
private String company;
private String email;
private String address;

public void setAddress(String address) {
this.address = address;
}

public void setCompany(String company) {
this.company = company;
}

public void setEmail(String email) {
this.email = email;
}

public void setAge(int age) {
this.age = age;
}

public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return String.format("Name: %s\n" +
"Age: %d\n" +
"Address: %s\n" +
"Company: %s\n" +
"E-mail: %s",
this.name, this.age, this.address, this.company, this.email);
}
}

2. 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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="mybean"
class="Inject"
p:name="Naman"
p:age="30"
p:address="Bangalore"
p:company="Personal"
p:email="naman.mehta@gmail.com"/>
</beans>


Code compilation needs three jar files in your classpath.
  1. Spring framework jar file
  2. commons-logging.jar
  3. xercesImpl-2.9.1.jar

I recommend, download latest eclipse IDE from http://www.eclipse.org for Java EE developers. Keep jar files in your path or download plug-ins for spring framework.



Will post other samples in next blog... Let me know your comments for the same...

Thursday, December 24, 2009

How to use Factory Method?

Overview:

The factory method pattern is an object-oriented design pattern. Like other creational patterns, it deals with the problem of creating objects (products) without specifying the exact class of object that will be created. The factory method design pattern handles this problem by defining a separate method for creating the objects, whose subclasses can then override to specify the derived type of product that will be created. More generally, the term factory method is often used to refer to any method whose main purpose is creation of objects.


How to create objects runtime?
  1. Write one Interface which has one method called myMethod().
  2. Write some set of Classes which implements this interface.
  3. Write one Class which has one static method which would return different objects based on the argument pass to that method.
Code Snap-shot:
  1. Interface code.

    1.1 myinterface.java
    public interface myinterface {
    public void myMethod();
    }
  2. Some set of Classes.

    2.1 a.java
    public class a implements myinterface {
    public a() {
    System.out.println("I am a...");
    }

    public void myMethod() {
    System.out.println("I am myMethod from a...");
    }
    }
    2.2 b.java
    public class b implements myinterface {
    public b() {
    System.out.println("I am b...");
    }

    public void myMethod() {
    System.out.println("I am myMethod from b...");
    }
    }
    2.3 b1.java
    public class b1 extends b {
    public b1() {
    System.out.println("I am b1...");
    }

    public void myMethod() {
    System.out.println("I am myMethod from b1...");
    }
    }
    2.4 b2.java
    public class b2 extends b {
    public b2() {
    System.out.println("I am b2...");
    }

    public void myMethod() {
    System.out.println("I am myMethod from b2...");
    }
    }

  3. Class which would return object based on the argument pass.

    3.1 test.java
    public class test {
    public test() {
    System.out.println("I am test class...");
    }

    public static myinterface getMyInterface(String ftpType) {
    try {
    if (ftpType.equals("doc")) {
    return (myinterface)Class.forName("a").newInstance();
    } else if (ftpType.equals("html")) {
    return (myinterface)Class.forName("b").newInstance();
    } else if (ftpType.equals("txt")) {
    return (myinterface)Class.forName("b1").newInstance();
    } else if (ftpType.equals("xml")) {
    return (myinterface)Class.forName("b2").newInstance();
    } else {
    return null;
    }
    } catch (Exception e) {
    System.out.println(e);
    return null;
    }
    }

    public static void main (String a[]) {
    myinterface m = test.getMyInterface("txt");
    m.myMethod();
    }
    }

Try this and happy blogging & reading. Any comment pls....