4:39 am - Tuesday April 25, 2017

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

BeforeAdvice.java:

package com.candidjava;

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

public class BeforeAdvice implements MethodBeforeAdvice {

	public void before(Method m, Object[] source, Object target) {
		System.out.println("  Running   BEFORE  Advice.....");

	}
}

DocWriter.java:

package com.candidjava;

public class DocWriter implements IWriter {

	@Override
	public String write() {

		return "This is Doc Writer ";
	}

}

IWriter.java:

package com.candidjava;

public interface IWriter {
	public String 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");

		Writer wObj = (Writer) appObj.getBean("writer");
		System.out.println(wObj.getWrite());

	}

}

PdfWriter.java:

package com.candidjava;

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

		return "This is Pdf Writer ";
	}
}

Writer.java:

package com.candidjava;

public class Writer {
	IWriter iw;

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

	public String getWrite() {
		return iw.write();
	}
}

XlsWriter.java:

package com.candidjava;

public class XlsWriter implements IWriter {
	@Override
	public String write() {
		return "This is Xls Writer ";
	}
}

beans.xml:

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

	<bean id="HelloAdvice" class="com.candidjava.BeforeAdvice">
	</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="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="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.