12:56 pm - Thursday July 27, 2017

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

   

 

 

BeforeAdvice.java:

package com.candidjava;

import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;

public class BeforeAdvice implements MethodBeforeAdvice {
	@Override
	public void before(Method method, Object[] args, Object target)
			throws Throwable {
		System.out.println("Before method!");
	}
}

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("Beforewriter");
		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.BeforeAdvice">
	</bean>

	<bean id="Beforewriter" 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+JARFILES


							
							
			
Filed in: Z-A Spring Codes

Comments are closed.