12:48 pm - Saturday July 22, 2017

Spring AOP Introduction Advice Example Program in Eclipse

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

Project Structure:

 

 

 

 

 

 

 

 Jar Files:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

How To Add Jar Files :

  

  

       

     

 AOPIntroduction.java:

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.support.DelegatingIntroductionInterceptor;

public class AOPIntroduction extends DelegatingIntroductionInterceptor
		implements Writer {

	private boolean locked = false;

	private Map methodCache = new HashMap();

	public boolean locked() {
		return locked;
	}

	public Object invoke(MethodInvocation invocation) throws Throwable {

		if (!locked) {
			if ((invocation.getMethod().getName().startsWith("set"))
					&& (invocation.getArguments().length == 1)) {
				Method getter = getGetter(invocation.getMethod());

				if (getter != null) {

					Object newVal = invocation.getArguments()[0];
					Object oldVal = getter.invoke(invocation.getThis(), null);

					if ((newVal == null) && (oldVal == null)) {
						locked = false;

					} else if ((newVal == null) && (oldVal != null)) {
						locked = true;

					} else if ((newVal != null) && (oldVal == null)) {
						locked = true;

					} else {
						locked = (!newVal.equals(oldVal));
					}
				}
			}
		}

		return super.invoke(invocation);
	}

	private Method getGetter(Method setter) {
		Method getter = null;
		getter = (Method) methodCache.get(setter);

		if (getter != null) {
			return getter;
		}
		String getterName = setter.getName().replaceFirst("set", "get");
		try {
			getter = setter.getDeclaringClass().getMethod(getterName, null);
			synchronized (methodCache) {
				methodCache.put(setter, getter);
			}
			return getter;
		} catch (NoSuchMethodException ex) {

			return null;
		}
	}
}

Bean.java:

public class Bean {

	private String name;

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

	public String getName() {
		return name;
	}
}

Main.java:

import org.springframework.aop.IntroductionAdvisor;
import org.springframework.aop.framework.ProxyFactory;

public class Main {

	public static void main(String[] args) {
		Bean target = new Bean();
		target.setName("Name");
		IntroductionAdvisor advisor = new WriterAdvisor();

		ProxyFactory pf = new ProxyFactory();
		pf.setTarget(target);
		pf.addAdvisor(advisor);
		pf.setOptimize(true);

		Bean proxy = (Bean) pf.getProxy();
		Writer proxyInterface = (Writer) proxy;

		System.out.println("Is instenceof Bean?: " + (proxy instanceof Bean));
		System.out.println("Is instenceof Writer?: "
				+ (proxy instanceof Writer));

		System.out.println("is locked?: " + proxyInterface.locked());
		System.out.println("is locked?: " + proxyInterface.locked());
		proxy.setName("Candid java");
		System.out.println("is locked?: " + proxyInterface.locked());

	}
}

Writer.java:

public interface Writer {
	boolean locked();
}

WriterAdvisor.java:

import org.springframework.aop.support.DefaultIntroductionAdvisor;

public class WriterAdvisor extends DefaultIntroductionAdvisor {

	public WriterAdvisor() {
		super(new AOPIntroduction(), Writer.class);
	}
}

Output:

 

 

 

 

 

 

 

 

 

 

 

 

DOWNLOAD+SOURCECODE+JARFILES


							
							
			
Filed in: Z-A All Java Codes

Comments are closed.