Popular Posts

Thursday, December 20, 2012

Spring AOP

Why spring aop advice is not triggering(invoking) for setter methods  invoked  by spring container ?


Problem :   The actual problem is explained by using below example.

 Bean class:
package org.example;

public class Triangle {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        System.out.println("In setter");
        this.name = name;
    }

}

Bean configuration  in spring.xml

<aop:aspectj-autoproxy />       

<bean id="triangle" class="org.example.Triangle" >
<property name="name" value="Right Angle Triangle"/>

</bean>
<bean id="firstaspect" class="org.example.aspects.SampleAspect"/>

Aspect class:

package org.example.aspects;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class SampleAspect {
    @Before("execution(public void setName(..))")
    public void testAdvice(JoinPoint joinPoint){
            System.out.println("Aspect Signature "+joinPoint.getSignature());
              
    }

}

spring client program:

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

public class AOPClient {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        Triangle triangle=ctx.getBean("triangle",Triangle.class);
             System.out.println("Triangle name: "+triangle.getName());

    }

}



output :
In setter
Triangle name: Right Angle Triangle

As per the spring aop concept the adviser  testAdvice must be called  before executing the setter method setName(String name) but  the adviser is not triggering.
because AOP Proxy works only after the bean complete initialization i.e after setting all the properties through DependecnyInjection.
after succesful initialization of bean if you invoke the setter method again from your client program then it'll trigger the advisor.

public static void main(String[] args) {
         ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
         Triangle triangle=ctx.getBean("triangle",Triangle.class);
        triangle.setName("Equilateral Triangle");

        System.out.println("Triangle name: "+triangle.getName());

   
    }

output:
In setter //This output line is called through spring container
Aspect Signature String org.example.Triangle.getName()
In setter //This output line is called through AOPClient Class
Triangle name:Equilateral Triangle




Monday, February 7, 2011

java.lang.UnsatisfiedLinkError


Exception in thread "main" java.lang.UnsatisfiedLinkError:
no cb in java.library.path

at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1413)
at java.lang.Runtime.loadLibrary0(Runtime.java:775)
at java.lang.System.loadLibrary(System.java:835)
at codebase.Code4jni.<init>(Code4jni.java:61)
at test.main(read.java:10)
This exception occurs when creating a new Code4jni object with CodeBase for the Java Programming Language. The exception occurs because the CodeBase native library (DLL on Windows) cannot be found. On UNIX and other systems, it can also be caused by a version difference between the run-time libraries on the system and the run-time libraries associated with with pre-built CodeBase native libraries. In addition, the -Xbootclasspath option will cause this.

Solution (Windows)

Locate the DLL32 folder. This folder should contain one or more DLLs. Each of these DLLs represents an index file format (FoxPro, dBASE, Clipper). Select the DLL that corresponds to your preferred index format. Rename that DLL to cb.dll and place it in your the library path. To determine your library path, you can run this piece of code:
class getLibraryPath
{
   public static void main(String[] args)
   {
      String path = System.getProperty("java.library.path");
      System.out.println(path);
   }
}

Explaining java.lang.OutOfMemoryError: PermGen space

If you get the exception java.lang.OutOfMemoryError: PermGen space

we need to set the following environment variable as follows.
Goto MyComputer - RightClick->Advanced tab- RightClick->EnvironmentVariables -RightClick->
SystemVariable-> Click on New button
and add the below.
Variable name is JAVA_OPTS and
Variable Value is -Xms1024m -Xmx1024m -XX:MaxPermSize=128m

Tuesday, January 25, 2011

Top 10 Exceptions In Real Time

There are 10 Exceptions that come freequently in real time usuage.
1.NullPointerEexception.
        -If we are trying to access a members(data variables or methods) of a class by using an object of the class,which has null reference.
Eg: public class NPETest{
    public static void main(String[] args){
        NPETest reference = null;
        reference.display();  
       // this statement will throw NullPointerException.
        
}
public static void display(){
   // some code goes here...  
}
}
2.ClassCastException.
3.NumberFormateException.
4.ArrayIndexOutOfBoundsException