11:08 am - Thursday April 27, 2017

Spring AOP Around Advice Dependency Injection 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.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.AfterReturningAdvice;

public class AroundAdvice implements MethodInterceptor {
	public Object invoke(MethodInvocation m) throws Throwable {
		System.out.println("     Before.....");

		m.proceed();
		System.out.println("AFTER .....");
		return null;
	}
}

DocWriter.java:


package com.candidjava;

public class DocWriter implements IWriter {

	@Override
	public void write() {

	}

}

IWriter.java:


package com.candidjava;

public interface IWriter {
	public void write();
}

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");
		IWriter wObj = (IWriter) appObj.getBean("writer1");
		wObj.write();
	}

}

PdfWriter.java:


package com.candidjava;

public class PdfWriter implements IWriter {
	@Override
	public void write() {

		System.out.println("This is pdf writer");

	}
}

Writer.java:


package com.candidjava;

public class Writer {
	IWriter iw;

	public void setWriteContent(IWriter writeContent) {
		this.iw = writeContent;
	}

	public void getWrite() {

	}
}

XlsWriter.java:


package com.candidjava;

public class XlsWriter implements IWriter {
	@Override
	public void write() {

	}
}

beans.xml:


<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>

	<bean id="HelloAdvice" class="com.candidjava.AroundAdvice">
	</bean>
	<bean id="writer1" class="org.springframework.aop.framework.ProxyFactoryBean">
		<property name="interceptorNames">

			<value>HelloAdvice</value>

		</property>
		<property name="proxyInterfaces">
			<value>com.candidjava.IWriter</value>
		</property>
		<property name="target">
			<ref local="pdfWriter" />
		</property>
	</bean>
	<bean id="pdfWriter" class="com.candidjava.PdfWriter"></bean>
	<bean id="xlxWriter" class="com.candidjava.XlsWriter"></bean>
	<bean id="docWriter" class="com.candidjava.DocWriter"></bean>

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

		<property name="writeContent">
			<ref local="writer1" />

		</property>
	</bean>

</beans>

OUTPUT:

DOWNLOAD +SOURCECODE + JARFILES

Filed in: Z-A All Java Codes

Comments are closed.