0% found this document useful (0 votes)
194 views

Spring AOP: Aspect Oriented Programming

This document discusses Spring AOP (Aspect Oriented Programming). It defines key AOP concepts like joinpoints, pointcuts, advice, and aspects. It provides examples of using AOP in Spring to add logging and transaction management cross-cutting concerns to application modules. The document explains how to configure AOP in Spring using beans, pointcut advisors, and proxy objects to weave advices into targets like methods.

Uploaded by

Ankit Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
194 views

Spring AOP: Aspect Oriented Programming

This document discusses Spring AOP (Aspect Oriented Programming). It defines key AOP concepts like joinpoints, pointcuts, advice, and aspects. It provides examples of using AOP in Spring to add logging and transaction management cross-cutting concerns to application modules. The document explains how to configure AOP in Spring using beans, pointcut advisors, and proxy objects to weave advices into targets like methods.

Uploaded by

Ankit Jain
Copyright
© © All Rights Reserved
Available Formats
Download as PPSX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Spring AOP

Aspect Oriented
Programming

www.java9s.com

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

Cross Cutting Concerns

www.java9s.com

Application Modules

Application Modules
Application Modules
Application Modules
Application Modules

Cross Cutting Concerns

Spring
Framework
www.java9s.com

Logging

Transaction
Application Modules
Application Modules
Application Modules

AOP Definitions.

Aspect
Joinpoint
Advice
Pointcut
Introduction
Target Object
AOP Proxy
Weaving

www.java9s.com

AOP Definitions.
Advice

Method

Joinpoints

www.java9s.com

Logger

Method

Transact
ion
Manager

Advisor

Method

Advisor

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

Advice Types

Before
Advice
After
returning
Advice
Around
Advice
www.java9s.com

Method
Method

Method
Method

Exception

AOP - Weaving
Compile time
Class Load Time
Runtime Springs way

Caller

www.java9s.com

Proxy

Targ
et

Pointcut and Advisor


POINTCUT CLASSES:
Perl5RegexpMethodPointcut
JdkRegexpMethodPointcut
Pointcut and Advisor in one class:
RegexpMethodPointcutAdvisor

www.java9s.com

Example
public class
CustomerImpl
implements
Customer{
public void browse(){
System.out.println("Brow
sing the internet");
}
}

www.java9s.com

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(Calc
ulate and bill the

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

Caller

Custom
er
Proxy

Target object
CustomerImpl
Customer
CafeOwner

proxyInterface

RegexpMethodPointcutAd
visor
* Apply to all methods

InternetAdvice implements
MethodBeforeAdvice

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

Configuration
Step 2: Configure the POINTCUT ADVISOR

<bean id ="cafeOwnerBeforeAndAfterAdvice"
class
="org.springframework.aop.support.RegexpMethodPoint
cutAdvisor">
<property name ="advice">
<ref local ="internetAdvice"/>
</property>
<property name ="pattern">
<value>.*</value>
</property>

</bean>

www.java9s.com

Configuration
Step 3: Configure the ProxyFactoryBean
<bean id ="customerProxy" class
="org.springframework.aop.framework.ProxyFactory
Bean">

<property name ="target">


<ref local ="customerImpl"/>
</property>
<property name ="proxyInterfaces">
<value>Customer</value>
</property>
<property name ="interceptorNames">
<list>
<value>cafeOwnerBeforeAndAfterAdvice</value>
</list>
</property>
www.java9s.com
</bean>

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

www.java9s.com

WWW.JAVA9S.COM

You might also like