140 likes | 366 Views
Spring Framework Integration. Lance Berry CIS 764 Fall 2006. The Spring Model. ORM. Web. …. Spring Integration with Other Components. Spring Framework. AOP. AspectJ. BeanFactory. POJO. Hibernate. Interface. POJI. Struts. Aspect Oriented Programming (AOP). What is AOP?
E N D
Spring Framework Integration Lance Berry CIS 764 Fall 2006
ORM Web ….. Spring Integration with Other Components Spring Framework AOP AspectJ BeanFactory POJO Hibernate Interface POJI Struts
Aspect Oriented Programming (AOP) • What is AOP? • AOP is an extension to Object-Oriented Programming. • Closely resembles Java Servlet Filters • AOP allows a developer to add testing or validation external to a class • Spring Supports both a native AOP and hooks into the AspectJ AOP Framework.
Aspect Oriented Programming Definitions and Terms • Aspect – modularization of a concern across multiple objects (called Cross-Cut Concerns in AOP). • Advice – Action taken by an Aspect • Joinpoint (called point cut in AOP) – Point defined during execution. • Target (or advised object) – Object being “advised” • AOP proxy – Object created to implement Aspect contracts and the primary method used in Spring’s AOP implementation. • Weaving – linking Aspects with other application types and objects.
Types of Advice • Before – processing before method invocation. Good for security or preprocessing validation. • After -- processing after method invocation. Good for verification or checking processing method accuracy • Around – Before and after method invocation. Good for checking changed values. • Throws – After method invocation, but only if an exception was raised • Introduction – special type of Advice that “Introduces” implementations to an object dynamically.
MessageWriter Code public class MessageWriter{public void writeMessage(){ System.out.println(“foobar”);} public void foo(){ System.out.println(“foo!”): } } }
MessageWrapper Code public aspect MessageWrapper{ private String prefix; private String suffix; public void setPrefix(String prefix){ this.prefix=prefix;} public void setSuffix(String suffix){ this.suffix=suffix;} pointcut doWriting(): execution(* MessageWriter.writeMessage()); before() : doWriting(){ System.out.println(prefix);} after() : doWriting(){ Sytem.out.printlng(suffix):} } }
AspectJ.XML <?xml version=“1.0” encoding=“UTF-8”?><!DOCTYPE beans PUBLIC “-//SPRING/DTD BEAN//EN” “http://www.springframework.org/dtd/spring-beans.dtd”> <beans> <bean id=“aspect” class=“MessageWrapper” factory-method=“aspectOf”> <property name=“prefix”> <value>Ha Ha!</value> </property> <property name=“suffix”> <value>Ho Ho!</value> </property> </bean> </beans>
Putting It All Together import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlAPplicationContext; Public class AspectJExample{ public static void main(String[] args){ Application context ctx= new FileSystemXmlApplicationContext(“AspectJ.xm”); MessageWriter writer = new MessageWriter(); writer.writeMessage(); writer.foo(); } }
The Output Ha Ha!foobar!Ho Ho!foo