11:58 am - Wednesday January 18, 2017

Spring AOP After Returning 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:

AfterAdvice.java:

package com.candidjava;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class AfterAdvice implements AfterReturningAdvice {

	@Override
	public void afterReturning(Object returnValue, Method method,
			Object[] args, Object target) throws Throwable {
		System.out.println("After Method");

	}

}

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("write");
		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.AfterAdvice">
	</bean>
	<bean id="write" 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="write" />

		</property>
	</bean>

</beans>

OUTPUT:

 

 

 

 

 

 

 

 

 

 

 

 

DOWNLOAD+SOURCECODE + JARFILES

Filed in: Z-A All Java Codes

Comments are closed.