Spring Tutorial Home

  • Managing Database Transactions
  • Remoting in Spring
  • Working with the Web Layer

Spring Home

Java Resources

Community

Site

Annotation Support in Spring AOP-1


Annotations have been introduced in Jdk1.5 (Tiger) release. This has changed the way we give deployment description of the components. Prior to annotation all the deployment description is given in XML files like we have seen <aop:config> tag in the previous examples. AspectJ annotations are the part of the AspectJ 5 release.


 Enabling @AspectJ support


 Before we actually use the AOP annotation we need to first enable them. This can be achieved by putting either one of the following statements in the Spring beans files.



  1. <aop:aspectj-autoproxy/>

  2. <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator" />


Example: -Lets take a logging example. Lets take an example of a banking application where we have account class. We have two methods in the class one is deposit and another one is withdrawal.


AccountService.java









package com.visualbuilder.aop;


public interface AccountService {


      void deposit(double amount)throws Exception;


      void withdrawal(double amount)throws Exception;


      public double getBalance();


      public void setBalance(double amount);


}



AccountServiceImpl.java









package com.visualbuilder.aop;


public class AccountServiceImpl implements AccountService {


      private double balance=0;


      public void deposit(double amount) throws Exception{


            if(amount >0){


               balance=balance+amount;


            }else{


                  throw new Exception("invalid amount");


            }


      }


      public void withdrawal(double amount)throws Exception {


            if(balance >0 && amount < balance){


                 balance=balance-amount;


            }else{


                 throw new Exception("insufficient balance");


            }     


      }


      public double getBalance() {


            return balance;


      }


      public void setBalance(double balance) {


            this.balance = balance;


      }


}


                    

Copyright © 2013 VisualBuilder. All rights reserved