Spring AOPAspect Oriented Programming<br />By<br />SrinivasReddy.S<br />www.java9s.com<br />
Cross Cutting Concerns<br />class Bank{<br />	private int balance;<br />   public void withdraw(int amount){<br />bankLogg...
Cross Cutting Concerns<br />Application Modules<br />Application Modules<br />Application Modules<br />Application Modules...
Cross Cutting Concerns<br />Logging<br />Transaction<br />Application Modules<br />Application Modules<br />Application Mo...
AOP – Definitions.<br /><ul><li>Aspect
Joinpoint
Advice
Pointcut
Introduction
Target Object
AOP Proxy
Weaving</li></ul>www.java9s.com<br />
AOP – Definitions.<br />Logger<br />Advice<br />Advisor<br />Method<br />Method<br />Method<br />Joinpoints<br />Advisor<b...
AOP - Definitions<br />Advice defines what needs to be applied and when.<br />Jointpoint is where the advice is applied.<b...
Advice Types<br />Before Advice<br />After returning Advice<br />Around Advice<br />Throws Advice<br />Method<br />Method<...
AOP - Weaving<br />Compile time<br />Class Load Time<br />Runtime – Springs way<br />Caller<br />Target<br />Proxy<br />ww...
Pointcut  and Advisor<br />POINTCUT CLASSES:<br />Perl5RegexpMethodPointcut<br />JdkRegexpMethodPointcut<br />Pointcut and...
Example<br />public class CustomerImpl implements Customer{<br />public void browse(){<br />System.out.println("Browsing t...
Upcoming SlideShare
Loading in …5
×

Spring AOP

1,012 views
872 views

Published on

Published in: Technology, News & Politics
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,012
On SlideShare
0
From Embeds
0
Number of Embeds
50
Actions
Shares
0
Downloads
0
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Spring AOP

  1. 1. Spring AOPAspect Oriented Programming<br />By<br />SrinivasReddy.S<br />www.java9s.com<br />
  2. 2. Cross Cutting Concerns<br />class Bank{<br /> private int balance;<br /> public void withdraw(int amount){<br />bankLogger.info(“Withdraw –”+amount);<br />tx.begin();<br /> balance = this.balance-amount;<br />accountDao.saveBalance(balance);<br />tx.commit();<br /> }<br />}<br />www.java9s.com<br />
  3. 3. Cross Cutting Concerns<br />Application Modules<br />Application Modules<br />Application Modules<br />Application Modules<br />Application Modules<br />www.java9s.com<br />
  4. 4. Cross Cutting Concerns<br />Logging<br />Transaction<br />Application Modules<br />Application Modules<br />Application Modules<br />Spring Framework<br />www.java9s.com<br />
  5. 5. AOP – Definitions.<br /><ul><li>Aspect
  6. 6. Joinpoint
  7. 7. Advice
  8. 8. Pointcut
  9. 9. Introduction
  10. 10. Target Object
  11. 11. AOP Proxy
  12. 12. Weaving</li></ul>www.java9s.com<br />
  13. 13. AOP – Definitions.<br />Logger<br />Advice<br />Advisor<br />Method<br />Method<br />Method<br />Joinpoints<br />Advisor<br />Transaction Manager <br />www.java9s.com<br />
  14. 14. AOP - Definitions<br />Advice defines what needs to be applied and when.<br />Jointpoint is where the advice is applied.<br />Pointcut is the combination of different joinpoints where the advice needs to be applied.<br />Aspect is applying the Advice at the pointcuts.<br />www.java9s.com<br />
  15. 15. Advice Types<br />Before Advice<br />After returning Advice<br />Around Advice<br />Throws Advice<br />Method<br />Method<br />Method<br />Method<br />Exception<br />www.java9s.com<br />
  16. 16. AOP - Weaving<br />Compile time<br />Class Load Time<br />Runtime – Springs way<br />Caller<br />Target<br />Proxy<br />www.java9s.com<br />
  17. 17. Pointcut and Advisor<br />POINTCUT CLASSES:<br />Perl5RegexpMethodPointcut<br />JdkRegexpMethodPointcut<br />Pointcut and Advisor in one class:<br />RegexpMethodPointcutAdvisor<br />www.java9s.com<br />
  18. 18. Example<br />public class CustomerImpl implements Customer{<br />public void browse(){<br />System.out.println("Browsing the internet");<br />}<br />}<br />class CafeOwner{<br /> void LogInTime(){<br />System.out.println(“Log In time and name of the customer”);<br />}<br /> void LogOutTime(){<br />System.out.println(“Log Out Time”);<br />}<br /> void issueUsageBill(){<br />System.out.println(“Calculate and bill the customer”);<br />}<br />}<br />www.java9s.com<br />
  19. 19. Before Advice -MethodBeforeAdvice<br />class InternetAdvisor implements MethodBeforeAdvice{<br />private CafeOwner cafeOwner;<br />public void before(Method arg0, Object[] arg1, Object arg2)<br />throws Throwable {<br />this.getCafeOwner().LogInTime();<br />}<br />}<br />www.java9s.com<br />
  20. 20. Customer Proxy<br />Caller<br />Target object<br />CustomerImpl<br />proxyInterface<br />Customer<br />RegexpMethodPointcutAdvisor<br />“*” – Apply to all methods<br />CafeOwner<br />InternetAdvice implements MethodBeforeAdvice<br />
  21. 21. Configuration<br />Step 1: Configure the Beans<br /><bean id ="customerImpl" class ="CustomerImpl"/><br /><bean id = "cafeOwner" class ="CafeOwner"/><br /><bean id ="internetAdvice" class ="InternetAdvice"><br /><property name ="cafeOwner" ref ="cafeOwner"/><br /></bean><br />www.java9s.com<br />
  22. 22. Configuration<br />Step 2: Configure the POINTCUT ADVISOR<br /><bean id ="cafeOwnerBeforeAndAfterAdvice" <br /> class ="org.springframework.aop.support.RegexpMethodPointcutAdvisor"><br /> <property name ="advice"><br /> <ref local ="internetAdvice"/><br /></property><br /><property name ="pattern"><br /><value>.*</value><br /></property><br /> </bean><br />www.java9s.com<br />
  23. 23. Configuration<br />Step 3: Configure the ProxyFactoryBean<br /><bean id ="customerProxy" class ="org.springframework.aop.framework.ProxyFactoryBean"><br /><property name ="target"><br /> <ref local ="customerImpl"/><br /> </property><br /> <property name ="proxyInterfaces"><br /> <value>Customer</value><br /> </property><br /> <property name ="interceptorNames"><br /> <list><br /> <value>cafeOwnerBeforeAndAfterAdvice</value><br /> </list><br /> </property><br /></bean><br />www.java9s.com<br />
  24. 24. Remember<br />Spring Does not support AOP for<br />Methods marked as final.<br />Fields<br />www.java9s.com<br />
  25. 25. WWW.JAVA9S.COM<br />

×