spring aop aspect oriented programming by srinivas reddy.s

18
Spring AOP Aspect Oriented Programming By Srinivas Reddy.S www.java9s.com

Upload: bridget-greer

Post on 18-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

Spring AOPAspect Oriented Programming

BySrinivas Reddy.S

www.java9s.com

Page 2: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

Cross Cutting Concerns

class Bank{private int balance;

public void withdraw(int amount){bankLogger.info(“Withdraw –”+amount);tx.begin();balance = this.balance-amount;accountDao.saveBalance(balance);tx.commit();}

}

www.java9s.com

Page 3: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

Cross Cutting Concerns

Application Modules

Application Modules

Application Modules

Application Modules

Application Modules

www.java9s.com

Page 4: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

Cross Cutting Concerns

Logging

Transaction

Application Modules

Application Modules

Application Modules

Spring Framework

www.java9s.com

Page 5: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

AOP – Definitions.

• Aspect• Joinpoint• Advice• Pointcut• Introduction• Target Object• AOP Proxy• Weaving

www.java9s.com

Page 6: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

AOP – Definitions.

Method Method Method

Logger

Transaction Manager

Joinpoints

Advice Advisor

Advisor

www.java9s.com

Page 7: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

AOP - Definitions

• Advice defines what needs to be applied and when.

• Jointpoint is where the advice is applied.• Pointcut is the combination of different

joinpoints where the advice needs to be applied.

• Aspect is applying the Advice at the pointcuts.

www.java9s.com

Page 8: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

Advice Types• Before Advice• After returning

Advice• Around Advice• Throws Advice

Method

Method

Method

Method

Exception

www.java9s.com

Page 9: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

AOP - Weaving

• Compile time• Class Load Time• Runtime – Springs way

TargetCaller Proxy

www.java9s.com

Page 10: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

Pointcut and Advisor

POINTCUT CLASSES:• Perl5RegexpMethodPointcut• JdkRegexpMethodPointcut

Pointcut and Advisor in one class:• RegexpMethodPointcutAdvisor

www.java9s.com

Page 11: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

Examplepublic class CustomerImpl

implements Customer{public void browse(){System.out.println("Browsing

the internet");}}

class CafeOwner{void LogInTime(){System.out.println(“Log In time and name of the customer”);}void LogOutTime(){System.out.println(“Log Out Time”);}void issueUsageBill(){System.out.println(“Calculate and bill the customer”);}

}www.java9s.com

Page 12: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

Before Advice -MethodBeforeAdvice

class InternetAdvisor implements MethodBeforeAdvice{

private CafeOwner cafeOwner;

public void before(Method arg0, Object[] arg1, Object arg2)throws Throwable {this.getCafeOwner().LogInTime();}}

www.java9s.com

Page 13: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

Customer

CustomerImpl

CafeOwner

InternetAdvice implements MethodBeforeAdvice

Customer Proxy

Caller

Target object

proxyInterface

RegexpMethodPointcutAdvisor“*” – Apply to all methods

Page 14: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

Configuration

Step 1: Configure the Beans• <bean id ="customerImpl" class ="CustomerImpl"/>• <bean id = "cafeOwner" class ="CafeOwner"/>• <bean id ="internetAdvice" class ="InternetAdvice">

– <property name ="cafeOwner" ref ="cafeOwner"/>

• </bean>

www.java9s.com

Page 15: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

Configuration

Step 2: Configure the POINTCUT ADVISOR• <bean id ="cafeOwnerBeforeAndAfterAdvice"

class ="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><property name ="advice"><ref local ="internetAdvice"/>

</property><property name ="pattern"><value>.*</value></property>

</bean>

www.java9s.com

Page 16: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

ConfigurationStep 3: Configure the ProxyFactoryBean<bean id ="customerProxy" class

="org.springframework.aop.framework.ProxyFactoryBean"><property name ="target"><ref local ="customerImpl"/></property><property name ="proxyInterfaces"><value>Customer</value></property><property name ="interceptorNames"><list><value>cafeOwnerBeforeAndAfterAdvice</value></list></property>

</bean>www.java9s.com

Page 17: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

Remember

• Spring Does not support AOP for– Methods marked as final.– Fields

www.java9s.com

Page 18: Spring AOP Aspect Oriented Programming By Srinivas Reddy.S

WWW.JAVA9S.COM