Spring AOP - DTD Example

Spring AOP - DTD Example

1.Create Account.java class that contains actual business logic.

public class Account {
	private double balance;

	public double getBalance() {
 return balance;
	}
	public void setBalance(double balance) {
 this.balance = balance;
	}
	public void withdraw(double amt) {
 balance = balance - amt;
 System.out.println("Withdraw Complted.Bal is : " + balance);
	}
	public void deposite(double amt) {
 balance = balance + amt;
 System.out.println("Deposite Complted.Bal is : " + balance);
	}
}

2.create advisor classes that implements above 4 mentioned Advice interfaces

//file: BeforeAdviceEx.java
public class BeforeAdviceEx implements MethodBeforeAdvice {
	@Override
	public void before(Method m, Object[] args, Object target) throws Throwable {
	System.out.println("1.Before Adice : Executed ******"); 
	}
}
=======================================================================
//file : AfterAdviceEx.java
public class AfterAdviceEx implements AfterReturningAdvice {
	@Override
	public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
 System.out.println("2. AFTER Advice Executed *****");
	}
}
=====================================================================
//file : AroundAdviceEx.java
public class AroundAdviceEx implements MethodInterceptor {
	@Override
	public Object invoke(MethodInvocation mi) throws Throwable {
 System.out.println("3.AROUND ADVICE ======");
 Object obj;
 System.out.println("----Before Business logic");
 obj = mi.proceed();
 System.out.println("----After Business logic");
 return obj;
	}
}
===============================================================================
//file : ThrowsAdviceEx.java
public class ThrowsAdviceEx implements ThrowsAdvice {
	public void afterThrowing(java.lang.ArithmeticException ex){  
        System.out.println("4.ThrowsAdvice : Error Occured!!!");  
    }   
}

3.Create SpringConfig.xml

  • create beans for Account class, four Advisor classes and for ProxyFactoryBean class.

  • ProxyFactoryBean class contains 2 properties target and interceptorNames.

    • target: The instance of Account class will be considered as target object.
    • interceptorNames: the instances of advisor classes. we need to pass the advisor object as the list object as in the xml file given above.
<!-- File : SpringConfig.xml -->
<beans>

	<bean id="acc" class="Account">
            <property name="balance" value="1000" />
	</bean>

	<bean id="beforeObj" class="BeforeAdviceEx"></bean>
	<bean id="afterObj" class="AfterAdviceEx"></bean>
	<bean id="aroundObj" class="AroundAdviceEx"></bean>
	<bean id="throwsObj" class="ThrowsAdviceEx"></bean>

<bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
 <property name="target" ref="acc"></property>
 <property name="interceptorNames">
 	<list>
              <value>beforeObj</value>
              <value>afterObj</value>
              <value>aroundObj</value>
              <value>throwsObj</value>
 	</list>
 </property>
</bean>

</beans>

4.Create AOPTest.java for testing the Application

public class AOPTest {
public static void main(String[] args) {
	 Resource res = new ClassPathResource("SpringConfig.xml");
     BeanFactory factory = new XmlBeanFactory(res);     
     Account account = (Account) factory.getBean("proxy");
     account.deposite(500);
}
}
======================output-==========================
1.Before Adice : Executed ******
3.AROUND ADVICE ======
----Before Business logic
Deposite Complted.Bal is : 1500.0
----After Business logic
2. AFTER Advice Executed *****

Note : here we are getting -proxy" bean object to apply AOP to the application