Cues-Spring Sample Application for Aspect Oriented Programming

IDE:Eclipse GanyMede
This Aspect i took to work out is logging.
Jars:spring-context-2.5.6.jar
spring-aop-2.5.6.jar
commons-logging-1.1.1.jar
aspectjweaver-1.6.2.jar
spring-2.5.6.jar
Steps:

  1. Create an interface Foo.java
  2. Create an java class that implements this interface.
  3. Create one class which include the methods that are used for Aspect Oriented Programming.
  4. Create one more class which contains the public static void main method.
  5. Create spring's application context.xml (here it is spring.xml)where the spring beans are configured and AOP is configured.(Will be explained in detail in the coming lines)
Foo.java(Interface)
package org.check;
public interface Foo {
void getAfter();
//void getBefore(String myName,String change);
//public void getBefore(SetFields fields,String myChange);
public void getBefore(SetFields fields,SetFields fields1,String myChange);
}



Default Foo Service.java(Class Implementing Above Interface)
package org.check;
public class DefaultFooService implements Foo {
public void getAfter() {
}
}



Main Class
package org.check;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class Boo {
public static void main(final String[] args) throws Exception {
BeanFactory ctx = new FileSystemXmlApplicationContext("E:/SECH_WS/TestLog/spring.xml");
Foo foo = (Foo) ctx.getBean("fooService");
foo.getAfter();
}
}
The line BeanFactory ctx = new FileSystemXmlApplicationContext"E:/SECH_WS/TestLog/spring.xml")
  • Bean factory class here we used is the spring container.
  • After loading the spring.xml into the container an instance of the bean is created.
  • The instance of the required class is obtained from getBean Method and referred there after.

SimpleProfiler.class

package org.check;
import org.check.BeanLogger;
public class SimpleProfiler {

public void afterMethod() throws Throwable {
System.out.println("afterMethod");}
public void beforeMethod(){
System.out.println("beforeMethod"); }

Now coming into the core part of the spring framework spring.xml.

spring.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"
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">
<bean id="fooService" class="org.check.DefaultFooService"/>
<bean id="profiler" class="org.check.SimpleProfiler"/>
<bean id="myprofiler" class="org.check.MyProfiler"/>
<aop:config>
<aop:aspect ref="profiler">
<aop:pointcut id="aopafterMethod" expression="execution(* org.check.DefaultFooService.*(..))"/>
<aop:pointcut id="aopBefore" expression="execution(* org.check.DefaultFooService.*(..))"/>
<aop:after pointcut-ref="aopafterMethod" method="afterMethod"/>
<aop:before pointcut-ref="aopBefore" method="beforeMethod"/>
<aop:aspect/>

<aop:aspect ref="myprofiler">
<aop:pointcut id="aopBeforeOne" expression="execution(* org.check.DefaultFooService.*(..))"/>
<aop:pointcut id="aopBeforeTwo" expression="execution(* org.check.DefaultFooService.*(..))"/>
<aop:aspect/>
<aop:config/>
beans>

Things configured in the spring.xml and their explanation.

  • Three Beans are configured i.e.Default Foo Service,Simple Profiler,My Profiler.
  • Simple Profiler and My Profiler are the classes which i have used for writing the Logging logic.
  • These classes have many methods and these methods are executed before/after execution of any other classes methods or the entire class.
  • refers to beginning and ending of an aspect.
  • Now the profiler what is defined as the bean is made an aspect.
  • Next we need to define Point-cuts.Point Cuts are nothing but declarative expressions stating on what classes methods we need to execute this Aspect(here my aspect is Logging) logic.
  • gives the information that the method afterMethod inside the SimpleProfiler.class must be executed on all methods which comes under the format (* org.check.DefaultFooService.*(..))" after the execution of the method
  • gives the information that the method beforeMethod inside the SimpleProfiler.class must be executed on all methods which comes under the format (* org.check.DefaultFooService.*(..))" before the execution of the method.

Now Run the application as Java Application.

Output:
beforeMethod
afterMethod

So the logging methods are executed everytime when the corresponding methods are executed.



No comments:

Post a Comment

My Space

Hang over--Wrath of Grapes