50
1 Spring & AOP Margus Jäger Lauri Tulmin

Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

1

Spring & AOP

Margus JägerLauri Tulmin

Page 2: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

2

Sissejuhatus

• 3. peatükk raamatus Spring in Action• 4. peatükk raamatus Professional Java

Development with the Spring Framework• Spring• Spring AOP• Võrdlus AspectJ’ga

Page 3: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

3

Spring

Page 4: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

4

Spring

• Kergekaaluline (Lightweight)• Kontrolli ümberpööramine (Inversion of

control)• Aspektidele orienteeritud (Aspect-oriented)• Konteiner• Raamistik (Framework)

Page 5: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

5

Hello World näide 1public interface IHello {

public void sayHello();}

public class Hello implements IHello {private IGreeting greeting;

public void setGreeting(IGreeting greeting) {this.greeting = greeting;

}

public void sayHello() {System.out.println(greeting.getGreeting());

}}

Page 6: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

6

Hello World näide 2public interface IGreeting {

public String getGreeting(); }

public class Greeting implements IGreeting {private String greeting;

public String getGreeting() {return greeting;

}

public void setGreeting(String greeting) {this.greeting = greeting;

}}

Page 7: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

7

Hello World näide 3<?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="hello" class="Hello"> <property name="greeting"> <ref local="greeting" /> </property> </bean> <bean id="greeting" class="Greeting"> <property name="greeting"> <value>hello world</value> </property> </bean></beans>

Page 8: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

8

Hello World näide 4public class Main {

public static void main(String[] args) {Resource res = new FileSystemResource("beans.xml");XmlBeanFactory factory = new XmlBeanFactory(res);IHello hello = (IHello)factory.getBean("hello");

hello.sayHello();}

}

Page 9: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

9

Spring & AOP

Page 10: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

10

Spring AOP

• Aspektid kirjutatakse javas• Ühendpunktideks on meetodid• Aspektide rakendamine programselt või

deklaratiivselt Springi konfiguratsioonifailis

Page 11: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

11

AOP Alliance

• http://aopalliance.sourceforge.net/

• Kõik Springis kasutatavad juhised realiseerivad AOP Alliance’i spetsifitseeritud või neist päritud liideseid

Page 12: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

12

Juhised

• Before• After returning• Around• Throws• Introduction

Page 13: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

13

Around juhispublic interface MethodInterceptor extends Interceptor {

Object invoke(MethodInvocation invocation) throws Throwable;

}

Page 14: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

14

public class PerformanceInterceptor implements MethodInterceptor { 

public Object invoke(MethodInvocation method) throws Throwable {

long start = System.currentTimeMillis();try {

Object result = method.proceed();return result;

}finally {

long end = System.currentTimeMillis();long timeMs = end - start;System.out.println("Method: " + method.toString() +

" took: " + timeMs +"ms.");}

}}

Page 15: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

15

Before juhispublic interface MethodBeforeAdvice extends BeforeAdvice { void before(Method m, Object[] args, Object target) throws

Throwable;}

Page 16: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

16

public class CountingBeforeAdvice implements MethodBeforeAdvice {

private int count;

public void before(Method m, Object[] args, Object target) { ++count; } public int getCount() { return count; }}

Before juhise näide

Page 17: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

17

After Returning juhispublic interface AfterReturningAdvice extends Advice { void afterReturning(Object returnValue, Method m, Object[] args, Object

target) throws Throwable;}

Page 18: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

18

public class CountingAfterReturningAdvice implements AfterReturningAdvice {

private int count;

public void afterReturning(Object returnValue, Method m, Object[] args, Object target) { ++count; } public int getCount() { return count; }}

After Returning juhise näide

Page 19: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

19

Throws juhis

org.springframework.aop.ThrowsAdvice

afterThrowing([Method, args, target,] Throwable)

Page 20: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

20

public static class ServletThrowsAdviceWithArguments implements ThrowsAdvice { public void afterThrowing

(Method m, Object[] args, Object target,ServletException ex) {

// Do something with all arguments } public void afterThrowing(RemoteException ex) throws Throwable { // Do something with remote exception }}

Throws juhise näide

Page 21: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

21

Lõikepunktid

• Ühendpunktid (JoinPoints) on Springis alati meetodid

• Lõikepunktid (Pointcuts) defineerivad, millistele meetoditele juhiseid rakendatakse

Page 22: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

22

Lõikepunktid Springis

• org.springframework.aop.support.NameMatchMethodPointcut

• org.springframework.aop.support.DynamicMethodMatcherPointcut

• org.springframework.aop.StaticMethodMatcherPointcut

• org.springframework.aop.support.JdkRegexpMethodPointcut

• org.springframework.aop.support.Perl5RegexpMethodPointcut

• org.springframework.aop.support.ControlFlowPointcut

• org.springframework.aop.support.ComposablePointcut

Page 23: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

23

NameMatchMethodPointcut klass

• NameMatchMethodPointcut addMethodName(String methodName) 

• void setMappedName(String methodName) • void setMappedNames(String methodName)

Page 24: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

24

NameMatchMethodPointcuti kasutamise näide Javas

Pointcut pc = new NameMatchMethodPointcut().addMethodName("setAge").addMethodName("setName");

Page 25: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

25

Regulaaravaldistega lõikepunktid<bean id="settersAndAbsquatulatePointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"> <property name="patterns"> <list> <value>.*get.*</value> <value>.*absquatulate</value> </list> </property></bean>

Page 26: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

26

Staatiline (ei sõltu argumentidest)public class MyBeanPointcut extends StaticMethodMatcherPointcut {

public boolean matches(Method theMethod, Class theClass) {

return (MyBean.class.isAssignableFrom(theClass) && theMethod.equals(...));

}

}

Page 27: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

27

Dünaamiline (arvestab argumente)public class MyBeanPointcut extends DynamicMethodMatcherPointcut {

public boolean matches(Method theMethod, Class theClass, Object[] arguments) {

boolean matches = false;

if (MyBean.class.isAssignableFrom(theClass) && theMethod.equals(...)) {

if (arguments[0].equals("Joe Smith")) { matches = true; }

}return matches;

}}

Page 28: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

28

ControlFlowPointcut

<bean id="servletPointcut"class="org.springframework.aop.support.ControlFlowPointcut">

<constructor-arg><value>javax.servlet.http.HttpServlet</value>

</constructor-arg>

</bean>

Page 29: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

29

Tehted lõikepunktidega

public static Pointcut union(Pointcut a, Pointcut b)

  kas a või b  

public static Pointcut intersection(Pointcut a, Pointcut b)

nii a kui b

Page 30: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

30

Juhtpunktid - Advisors

• (pakkuge hea tõlge)• Ühendavad Springis lõikepunktid ja

juhised aspektiks• Võivad sisaldada lõikepunktide

defineerimiseks vajalikke meetodeid (pole vaja lõikepunktide jaoks eraldi klasse defineerida)

Page 31: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

31

DefaultPointcutAdvisorpublic class DefaultPointcutAdvisor {

private Pointcut pointcut;

private Advice advice;

public Pointcut getPointcut() { return pointcut; }

public Advice getAdvice() { return advice; }

public void setPointcut(Pointcut pc) { pointcut = pc; }

public void setAdvice(Advice a) { advice = a; }

}

Page 32: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

32

DefaultPointcutAdvisor (2)

<bean name ="myAdvisor“ class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="pointcut">

<ref local="myPointcut"/> </property> <property name="advice">

<ref local="myAdvice"/> </property></bean>

Page 33: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

33

NameMatchMethodPointcutAdvisor

<bean name="advisor1" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="advice" ref="beforeAdviceA"/> <property name="mappedName" value=“myMethod"/></bean>

Page 34: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

34

RegexpMethodPointcutAdvisor<bean id=“advisor2" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">        <property name="advice">

<ref local="myAdvice"/> </property>

<property name="patterns"> <list> <value>.*set.*</value> <value>.*getName</value> </list> </property></bean>

Page 35: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

35

AOP realiseerimine

• Kompileerimisel (AspectJ)• Klassi laadimisel• Programmi käimisel (Spring)

Page 36: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

36

Staatiline vs dünaamiline• Statiline AOP- Kompileerimisel põimitakse aspektid java

klassidesse- AspectJ• Dünaamiline AOP- Aspektid põimitakse programmi töö ajal- Klasse pole vaja uuesti kompileerida- Aspektide jaoks vahendaja (proxy)- Spring

Page 37: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

37

Vahendajad (Proxy)

• JDK Dynamic Proxy- Rakendatav objektidele, mille klassid

realiseerivad liideseid• CGLIB Proxy- Lennult genereeritakse antud klassi

laiendav klass

Page 38: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

38

ProxyFactoryBean

• Loob vahendaja-objeti, millele rakendatakse aspekte

• Saab siduda objektiga nii juhiseid (advice) kui juhtpunkte (advisor)

Page 39: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

39

<bean id="myDependency1"class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="target"><ref local="myDependencyTarget"/>

</property><property name="interceptorNames">

<list><value>myAdvice</value><value>myAdvisor</value>

</list></property>

</bean>

Page 40: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

40

Automaatsed vahendajad

• BeanNameAutoProxyCreator• DefaultAdvisorAutoProxyCreator

Page 41: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

41

<bean id="proxyCreator"class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">

<property name="beanNames"><list>

<value>foo*</value><value>barBean</value>

</list></property>

<property name="interceptorNames"><list>

<value>advice</value></list>

</property></bean>

Page 42: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

42

DefaultAdvisorAutoProxyCreator

• Juhtpunkte rakendatakse kõikidele objektidele

<bean id="aapc" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/ >

Page 43: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

43

Spring AOP vs AspectJ

Page 44: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

44

Spring ja AspectJ võrdlus

Spring AOP• Lihtne seadistada• Käimise ajal• AOP Alliance’i

liidesed• Vähem võimalusi

AspectJ• Väheke keerulisem• Kompileerimisel• Eraldi

programmeerimiskeel• Rohkem võimalusi

Page 45: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

45

Süntaks- Spring AOP (AOP Alliance)public class SimpleMethodInterceptor implements MethodInterceptor {

public Object invoke(MethodInvocation invocation)throws Throwable {...return invocation.proceed();

}}- AspectJpublic aspect SimpleServiceAroundAspect {

pointcut serviceExecution():execution(public * ee.bus.*Service.*(..));

Object around(): serviceExecution() {...return proceed();

}}

Page 46: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

46

Spring AOP piirangud

• Ühenduspunktideks ainult meetodid• Ilma CgLibita aspektid ainult liideste

meetoditel• private, final, static meetoditele ei saa

aspekte panna• Aspektid rakendatakse ainult konteineri

poolt loodud objektidele

Page 47: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

47

Valmis aspektid

• Transaktsioonid• Puulimine• ACEGI security

Page 48: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

48

Jõudlus

Page 49: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

49

Spring AOP ja AspectJ jõudlus

autor: Rein Raudjärv

1 2 3 4 5

410 470 530 590 650

411

27 40 52

24 6 8 10

1

10

100

1000

Invocation Time [ms]

Number of Aspects

1 Million Method Invocations (Adding Numbers)

AspectJ (Before/After) AspcetJ (Around) Spring AOP

Page 50: Spring & AOP - ut · 2006-03-22 · Development with the Spring Framework • Spring • Spring AOP • Võrdlus AspectJ’ga. 3 Spring. 4 Spring • Kergekaaluline (Lightweight)

50

Küsimused