12:03 pm - Wednesday January 18, 2017

Spring AOP Around 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:

AroundAdvice.java:

package com.candidjava;

import java.util.Arrays;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class AroundAdvice implements MethodInterceptor {
	@Override
	public Object invoke(MethodInvocation methodInvocation) throws Throwable {

		// System.out.println("Method name : "+
		// methodInvocation.getMethod().getName());
		// System.out.println("Method arguments : "+
		// Arrays.toString(methodInvocation.getArguments()));

		// same with MethodBeforeAdvice
		System.out.println("Before method ");

		// proceed to original method call
		Object result = methodInvocation.proceed();

		// same with AfterReturningAdvice
		System.out.println(" after method ");

		return result;

	}
}

Main.java:

package com.candidjava;

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

public class Main {

	public static void main(String[] args) {

		ApplicationContext appObj = new FileSystemXmlApplicationContext(
				"Config\\beans.xml");
		Writer wObj = (Writer) appObj.getBean("AroundWriter");
		wObj.getName();
		System.out.println();
	}

}

Writer.java:

package com.candidjava;

public class Writer {

	private String name;

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

	public void getName() {

		System.out.println("This is " + this.name);
	}
}

Beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>

	<bean id="writer" class="com.candidjava.Writer">

		<property name="name" value="Deepa" />

	</bean>

	<bean id="HelloAdvice" class="com.candidjava.AroundAdvice">
	</bean>

	<bean id="AroundWriter" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="interceptorNames">
			<list>
				<value>HelloAdvice</value>
			</list>
		</property>

		<property name="target" ref="writer" />

	</bean>

</beans>

OUTPUT:


DOWNLOAD+SOURCECODE+JARFILE


							
							
			
Filed in: Z-A Spring Codes

Comments are closed.