Spring AOP Aspect Oriented Programming By Srinivas Reddy

  • Slides: 18
Download presentation
Spring AOP Aspect Oriented Programming By Srinivas Reddy. S www. java 9 s. com

Spring AOP Aspect Oriented Programming By Srinivas Reddy. S www. java 9 s. com

Cross Cutting Concerns class Bank{ private int balance; public void withdraw(int amount){ bank. Logger.

Cross Cutting Concerns class Bank{ private int balance; public void withdraw(int amount){ bank. Logger. info(“Withdraw –”+amount); tx. begin(); balance = this. balance-amount; account. Dao. save. Balance(balance); tx. commit(); } } www. java 9 s. com

Cross Cutting Concerns Application Modules Application Modules www. java 9 s. com

Cross Cutting Concerns Application Modules Application Modules www. java 9 s. com

Cross Cutting Concerns Logging Transaction Application Modules Spring Framework www. java 9 s. com

Cross Cutting Concerns Logging Transaction Application Modules Spring Framework www. java 9 s. com

AOP – Definitions. • • Aspect Joinpoint Advice Pointcut Introduction Target Object AOP Proxy

AOP – Definitions. • • Aspect Joinpoint Advice Pointcut Introduction Target Object AOP Proxy Weaving www. java 9 s. com

AOP – Definitions. Advice Method Joinpoints www. java 9 s. com Logger Method Transaction

AOP – Definitions. Advice Method Joinpoints www. java 9 s. com Logger Method Transaction Manager Advisor Method Advisor

AOP - Definitions • Advice defines what needs to be applied and when. •

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. java 9 s. com

Advice Types • Before Advice • After returning Advice • Around Advice • Throws

Advice Types • Before Advice • After returning Advice • Around Advice • Throws Advice Method Exception www. java 9 s. com

AOP - Weaving • Compile time • Class Load Time • Runtime – Springs

AOP - Weaving • Compile time • Class Load Time • Runtime – Springs way Caller www. java 9 s. com Proxy Target

Pointcut and Advisor POINTCUT CLASSES: • Perl 5 Regexp. Method. Pointcut • Jdk. Regexp.

Pointcut and Advisor POINTCUT CLASSES: • Perl 5 Regexp. Method. Pointcut • Jdk. Regexp. Method. Pointcut and Advisor in one class: • Regexp. Method. Pointcut. Advisor www. java 9 s. com

Example public class Customer. Impl implements Customer{ public void browse(){ System. out. println("Browsing the

Example public class Customer. Impl implements Customer{ public void browse(){ System. out. println("Browsing the internet"); } } www. java 9 s. com class Cafe. Owner{ void Log. In. Time(){ System. out. println(“Log In time and name of the customer”); } void Log. Out. Time(){ System. out. println(“Log Out Time”); } void issue. Usage. Bill(){ System. out. println(“Calculate and bill the customer”); } }

Before Advice -Method. Before. Advice class Internet. Advisor implements Method. Before. Advice{ private Cafe.

Before Advice -Method. Before. Advice class Internet. Advisor implements Method. Before. Advice{ private Cafe. Owner cafe. Owner; public void before(Method arg 0, Object[] arg 1, Object arg 2) throws Throwable { this. get. Cafe. Owner(). Log. In. Time(); } } www. java 9 s. com

Caller Customer Proxy Target object Customer. Impl Customer Cafe. Owner e proxy. Interfac Regexp.

Caller Customer Proxy Target object Customer. Impl Customer Cafe. Owner e proxy. Interfac Regexp. Method. Pointcut. Advisor “*” – Apply to all methods Internet. Advice implements Method. Before. Advice

Configuration Step 1: Configure the Beans • <bean id ="customer. Impl" class ="Customer. Impl"/>

Configuration Step 1: Configure the Beans • <bean id ="customer. Impl" class ="Customer. Impl"/> • <bean id = "cafe. Owner" class ="Cafe. Owner"/> • <bean id ="internet. Advice" class ="Internet. Advice"> – <property name ="cafe. Owner" ref ="cafe. Owner"/> • </bean> www. java 9 s. com

Configuration Step 2: Configure the POINTCUT ADVISOR • <bean id ="cafe. Owner. Before. And.

Configuration Step 2: Configure the POINTCUT ADVISOR • <bean id ="cafe. Owner. Before. And. After. Advice" class ="org. springframework. aop. support. Regexp. Method. Pointcut. Advisor"> <property name ="advice"> <ref local ="internet. Advice"/> </property> <property name ="pattern"> <value>. *</value> </property> </bean> www. java 9 s. com

Configuration Step 3: Configure the Proxy. Factory. Bean <bean id ="customer. Proxy" class ="org.

Configuration Step 3: Configure the Proxy. Factory. Bean <bean id ="customer. Proxy" class ="org. springframework. aop. framework. Proxy. Factory. Bean"> <property name ="target"> <ref local ="customer. Impl"/> </property> <property name ="proxy. Interfaces"> <value>Customer</value> </property> <property name ="interceptor. Names"> <list> <value>cafe. Owner. Before. And. After. Advice</value> </list> </property> </bean> www. java 9 s. com

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

Remember • Spring Does not support AOP for – Methods marked as final. – Fields www. java 9 s. com

WWW. JAVA 9 S. COM

WWW. JAVA 9 S. COM