Spring Framework

Spring Projects

Spring Project 1

adplus-dvertising
Example of MyFramework AOP Implementation
Previous Home Next

Introduction: An Aspect Oriented Programming (AOP) is a programming model that compliments object oriented programming model and provides an efficient mechanism of implementing behaviors which have crosscutting concerns.

Technology use to run this source code

  1. Spring 2.5 jar files
  2. Eclipse IDE
  3. Tomcat Server

Source Code:

applicationContext.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-2.5.xsd">
<bean id="t" class="org.r4r.client.One"/>
<bean id="a" class="org.r4r.client.BeforeOne"/>
<bean id="proxy" class="org.r4r.framework.ProxyFactory">
<property name="target" ref="t"/>
<property name="advisor" ref="a"/>
</bean>
</beans>

BeforeAdviceHandler.java

package org.r4r.framework;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class BeforeAdviceHandler implements InvocationHandler {
Object target;
BeforeMethod advisor;
public BeforeAdviceHandler(Object target, BeforeMethod advisor) {
super();
this.target = target;
this.advisor = advisor;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
advisor.doBefore();
return method.invoke(target, args);
}
}

BeforeMethod.java

package org.r4r.framework;
public interface BeforeMethod {
public void doBefore();
}

ProxyFactory.java

package org.r4r.framework;
import java.lang.reflect.Proxy;
import org.springframework.beans.factory.FactoryBean;
public class ProxyFactory implements FactoryBean {
Object target;
BeforeMethod advisor;
   @Override
public Object getObject() throws Exception {
BeforeAdviceHandler handler=new BeforeAdviceHandler(target, advisor);
Object proxy=Proxy.newProxyInstance
(target.getClass().getClassLoader(), target.getClass().getInterfaces(), handler);
return proxy;
}
@Override
public Class<Proxy> getObjectType() {
return Proxy.class;
}
@Override
public boolean isSingleton() {
return true;
}
public void setTarget(Object target) {
this.target = target;
}
public void setAdvisor(BeforeMethod advisor) {
this.advisor = advisor;
}
}

One.java


package org.r4r.client;
public class One implements Showable {
@Override
public void show() {
System.out.println("Show() method of One invoked..");
}
}

Showable.java

package org.r4r.client;
public interface Showable {
public abstract void show();
}

BeforeOne.java

package org.r4r.client;
import org.r4r.framework.BeforeMethod;
public class BeforeOne implements BeforeMethod {
@Override
public void doBefore() {
System.out.println("Before advice is applied.");
}
}

ProxyTest.java

package org.r4r.client;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class ProxyTest {
public static void main(String[] args) {
Resource r=new ClassPathResource("applicationContext.xml");
BeanFactory f=new XmlBeanFactory(r);
System.out.println("Obtaining proxy of one..");
Showable p=(Showable)f.getBean("proxy");
System.out.println("Proxy obtaining invoked.");
p.show();
}
}

output:

Obtaining proxy of one..
Proxy obtaining invoked.
Before advice is applied.
Show() method of One invoked..
Previous Home Next