Thursday, October 13, 2011

Short description of Spring AOP.

AOP stand for Aspect Oriented Programming 

Aspect - It is concept which a developer wants to implements globally i.e. Think of this as the general feature you want to apply globally to your application (logging etc).
Advice - Code that is used to implement Aspect. A chunk of code that is invoked during program execution, and is a piece of the logic for implementing your aspect.
Joinpoint - A single location in the code where an advice should be executed (such as field access, method invocation , constructor invocation, etc.).
Pointcut - A pointcut is a set of many joinpoints where an advice should be executed.
Targets/Target Objects - The objects you want to apply an aspect or set of aspects to
Introduction - This is the ability to add methods to an object.

Lets take a simple example

package springExample;

public interface Adder {

    public int add(int a,int b);


==Implementation class

package springExample;

public class AdderImpl implements Adder {

    public int add(int a, int b){
        return a+b;

==Execution class

package springExample;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;

public class AdderTest {

    public static void main(String args[]){

        Resource resource = new FileSystemResource("src/SpringHelloWorld.xml");    
        BeanFactory factory = new XmlBeanFactory(resource);
       // XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("SpringHelloWorld.xml"));
        Adder adder = (Adder)factory.getBean("adder");
        int result = adder.add(10,11);
        System.out.println("Result = " + result);

        result = adder.add(0,0);
        System.out.println("Result = " + result);
package springExample;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
public class LogAfterReturningAdvice implements AfterReturningAdvice{
    public void afterReturning(Object returnValue, Method method, Object[] args,
    Object target) throws Throwable {
        System.out.println("After Normal Return from Method");
package springExample;

import java.lang.reflect.Method;

import org.springframework.aop.ThrowsAdvice;
public class LogAfterThrowsAdvice implements ThrowsAdvice{
    public void afterThrowing(Method method, Object[] args, Object target,
    Exception exception){
        System.out.println("Exception is thrown on method " + method.getName());
==Around Advice

package springExample;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import org.aopalliance.intercept.MethodInvocation;

public class LogAroundAdvice implements MethodInterceptor{
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        Object arguments[] = methodInvocation.getArguments();
        int number1 = ((Integer)arguments[0]).intValue();
        int number2 = ((Integer)arguments[1]).intValue();
        if (number1 == 0 && number2 == 0){
            throw new Exception("Dont know how to add 0 and 0!!!");
        return methodInvocation.proceed();

public Object intercept(Object arg0, Method arg1, Object[] arg2,
MethodProxy arg3) throws Throwable {
// TODO Auto-generated method stub
return null;
==Before Advice

package springExample;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;
public class LogBeforeCallAdvice implements MethodBeforeAdvice
    public void before(Method method, Object[] args, Object target) {
        System.out.println("Before Calling the Method>>>>>>>>>>>..");
        for(int i=0;i
        System.out.println("target>>>>>>>>>>>>>>" + target.getClass().getName());

===========Out Put

Before Calling the Method>>>>>>>>>>>..
After Normal Return from Method
Result = 21
Before Calling the Method>>>>>>>>>>>..
After Normal Return from Method
Result = 0

Short Description of EJB 1.1 J2EE(Java 2 Enterprise Edition ) .

J2EE(Java 2 Enterprise Edition )
It is the big concept having large nos of technology in it such as EJB,JMS,JAVA IDL etc
Here we will talk about the EJB(Enterprise Java Bean) only
EJB(Enterprise Java Bean) : This is a J2EE technology which is use to prepare a Bean(Known as the server side software component )for the Java enterprise edition.
Now what the Enterprise edition stand for:
Enterprise edition stand for the 3 tier architecture (CLIENT-MIDDLEWARE-DATABSE)
Bean is server side component and it is deployed in a container that are placed in the Application server.
What is Container and AS(Application Server):
Container : Generally the Bean(Sever side software component ) is logic which we want to be perform for us in the Business but it requires some platform to be operated and that plate form is given by the container and it is in the AS.
Now in one AS there can be many container and in one container there can be many beans
But before starting the Concept of the Beans there are certain think that has to been needed to understand some of the concept of the JAVA they are as follows:
8)Java IDL
9)JAVA Mail
10)XML(Extensible mark up language)
1) Java RMI: this is the facilities which is provided by the java for the invocation of the remote method (the method which is not present in our M/c or in our JVM with the help of the Interface.
2) JNDI: this include the Naming directory which is used for the look up the remote component ie beans during the operation.
3) JDBC : it is for the database connection and it is similar to the ODBC which is provided by the Microsoft.
4) JTA and JTS: this both are the api produce by the sun to help to create the Transaction Management which is of great important for the AS further explained.
5) JMS : this is for the Messaging service which deals with the transfer of message between the object which required during the application as we required that our beans talk with each other to facilitate the application.
6) JSP: this is the scripting language use to built the pages in the java language. It is similar to the servlets infect they are complied into the servlets. only the difference is that JSP is not truly 100% java pages they are concentrated on the look and feel factors
7) Java IDL: this is the beautiful concept with can be simply define as implementation of CORBA in java. ie(it is the specific implementation of CORBA in java side)
8) Java Mail: it is use for the mailing service and is not much imp for us.
9) Connector: To ask.

10) XML : this is the language which is having the different concept for the different application for EJB it work as the descriptor and of the JSP it work as the data document format for authoring web script.
Now lets talk about the Container and The AS(Application Server)
(Application Server)As provided the following things
Resource management – deal with the database connection,pool,threads,socket conection
Life cycle:
State management :it look the state of the bean (share the same state bean with the client)
Transaction:Allow the user to use the same data access at the same time if any one change then that data is changed (Whole or Rollback) .
Security: which user is allowed to see the bean and who is blocked
Persistence :It maintain the Data in the Formate which can be easily read and can I\be out in the Db at the definite point of time.(It hold the data at one point of time write it back to the Db at the other point of time)
Remote accessibility: This the concept that is much imp for the Enterprise edition as the client is now knowing that the component to which contact is either lying on its own machine or it is residing on the other M/c .
So it is much important to select the proper AS as it perform most of the typical job for the J2EE which client is not to be worried . There are different AS available in the market BEA weblogic ,Oracle Oracle9ias ,IBM Websphere etc etc
All have there own concept and Container but is developed under the common API of the J2ee.
Now what is the different between Container and the AS:
Theoretically it is difficult to make the difference between the AS and the container but we can say that beans are placed in the Container and Container is Placed in the AS
(More over it is also clear that container of company cannot be used in other AS ie container used by the weblogic cannot be used by the Oracle9ias.)
Now we will talk about the EJB:
It has two version EJB 1.0 and EJB1. 1 the main difference is that EJB1.1 has the concept of ENTITY Beans.
􀃆 Manage by two ways
a)Container Manage Persistence.
b)Bean Manage Persistence.

The word session itself indicate that it define the Session for a particular Client. Generally the Business logic which we want to deal with is written in this Beans.
Characteristic of Session Beans are:
1)They cannot be share that means that Once it is allocated to a Particular client then unless and until it is removed (set free from the client ) it is not been available to the other (No two client can share the same Session bean at the same time)
2)It can talk to the Entity beans for its fulfillment of purpose .
3)It can talk with 2 or more beans for this.
Type of the Session Beans:
This is the Session Bean which maintain its state in normal manner speaking that this type of the Bean is used in e-commerce application in which you want that the client should know what he had purchase in given interval of time.
If the client is purchasing the item and again go to the next counter to purchase the next item and if he want to know what he had purchase in the previous counter he can use this type of beans in which the state of the session beans is maintained and if required this state full beans is given to the user with full state having the information of the previous encounter.
This is the Session Bean which did not maintain the state for it use. This type of the application is generally used where the back information is of no use. Ie the validation of the visa card or the credit card only thing to do is take the card no and its user signature and validate it with the data base data and return true or false or it can be used in the application of transferring the mail to the client about the receiving of the amount or cheque.
Why the concept of the Entity beans comes in to the picture when we are having the concept of the session beans. The main reasons is that we want the persistence in our data and to maintain the transaction fast and easily. It generally define the Data at one point of time and write back the data to the database in the next movement of time. So if the system is crashed the data can be easily available from the Entity beans And it is sure that Entity beans is inherently STATFUL as the data maintained it state.
Now there are two ways to handle the Entity Beans:
1)Container Manage Persistence:
2)Bean Manage Persistence:

1)Container Manage Persistence:
As the name itself suggest that in this the overall command of the Beans is in the hand of the Container in which it is deployed. Not much versatility is available for the user in this management . In coding section we can say that in container manage persistence the client is not required to write the SQL quarries the container use its internal inbuild quarries for fetching and manipulating the result.
2)Beans Manage Persistence:
In this the Entity beans is manage by itself . here the user write it own querries for fetching of the result and manipulating the result it has high versatility than the Container manage persistence
Characteristic of the Entity beans
1)It can be shared between the user ie. Two user can use the same entity beans at the same time.
2)It maintain the Data persistence.
3)It maintain the state as it by default STATEFUL Beans
Q)Then what is the main difference between Session and Entity Beans
1)The session beans cannot be Shared but the Entity Beans can be shared.
2)There can be relation between entity beans ie. one entity bean can have the internal relation with other entity beans but this is not possible with Session beans.
3)Entity always maintain the State but in case of Session only the STATEFUL Session beans maintain the state.
4)Entity beans maintain the persistence of the data. Where as the session didn’t.
For Entity Beans: Beans manage persistence
Activate()—container-no need for code
Lode(select)—pk- this.entityContext.getPrimaryKey()
1)connection is obtained
2)Prepared statement
3)select query
Store(update)—pk- this.entityContext.getPrimaryKey()
1)connection is obtained
2)Prepared statement
3)update query

Passivate()-Container— no need for code
Create(insert)—argument gives the id
1)connection is obtained
2)Prepared statement
3)Insert query
Remove(delete)—pk- this.entityContext.getPrimaryKey()
1)connection is obtained
2)Prepared statement
3)Delete query