After throwing advice
- AfterThrowing advice will execute after the method throws an exception.
We should implement ThrowsAdvice
interface. As this interface is a tag interface (marker interface) for
throws advice means no methods are available in this interface as methods are
invoked by reflection.
So, Implementing classes must implement method of the below form:
void afterThrowing([Method, args,
target], ThrowableSubclass);
·
Some examples of valid methods are:-
public void afterThrowing(Exception
ex)
public void
afterThrowing(RemoteException)
public void afterThrowing(Method
method, Object[] args, Object target, Exception ex)
- If a throws-advice method throws an exception itself, it will override the original exception (i.e. change the exception thrown to the user). The overriding exception will typically be a RuntimeException; this is compatible with any method signature
Syntax Of
ThrowsAdvice— afterThrowing () Method
public void
afterThrowing(IllegalTransactionStateException itse)
throws Throwable {
}
Steps for creating implementation
for ThrowsAdvice :-
1) Create a java file in com.gaurav.aop.advices package and name it as TraceLogAfterMethodThrowException.
2) Place the below code in that java file.
3) Create a java bean UserTransaction in the com.gaurav.aop.services package and place the below code in the bean file.
4) Create a xml bean file named spring-user-transaction-beans-afterthrows.xml inside src folder and place the below code in that file
5) Create a java file TraceLogMethodThrowsAdviceClient.java
in the package com.gaurav.aop.advices.client inside src folder.Place the below
available code in that file.
TraceLogAfterMethodThrowException.java
package com.gaurav.aop.advices;
import
org.springframework.aop.ThrowsAdvice;
import org.springframework.transaction.IllegalTransactionStateException;
public class
TraceLogAfterMethodThrowException implements ThrowsAdvice {
public void
afterThrowing(IllegalTransactionStateException itse)
throws Throwable {
System.out
.println("TraceLogAfterMethodThrowException:- Entered after method throws
exception ");
System.out.println("Execption is - " +
itse.getMessage());
}
}
UserTransaction.java
package com.gaurav.aop.services;
import org.springframework.transaction.IllegalTransactionStateException;
public class UserTransaction {
private Long
transactionId;
private String
userName;
private String
productPurchased;
private String
transactionStatus;
private String
message;
public Long
getTransactionId() {
return transactionId;
}
public void
setTransactionId(Long transactionId) {
this.transactionId = transactionId;
}
public String
getUserName() {
return userName;
}
public void
setUserName(String userName) {
this.userName = userName;
}
public String
getProductPurchased() {
return productPurchased;
}
public void
setProductPurchased(String productPurchased) {
this.productPurchased = productPurchased;
}
public String
getTransactionStatus() {
return transactionStatus;
}
public void
setTransactionStatus(String transactionStatus) {
this.transactionStatus = transactionStatus;
}
public String
getMessage() {
return message;
}
public void
setMessage(String message) {
this.message = message;
}
public void
getException() {
throw new IllegalTransactionStateException(message);
}
}
spring-user-transaction-beans-afterthrows.xml
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean
id="userTransaction"
class="com.gaurav.aop.services.UserTransaction">
<property name="transactionId"
value="145678622322" />
<property name="userName" value="Kumar
Gaurav" />
<property name="productPurchased"
value="Galaxy S4" />
<property name="transactionStatus"
value="Failed" />
<property name="message" value="Failed due
to IllegalTransactionStateException" />
</bean>
<bean
id="traceLogAfterMethodThrowExceptionBean"
class="com.gaurav.aop.advices.TraceLogAfterMethodThrowException"
/>
<bean
id="userTransactionProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="userTransaction" />
<property name="interceptorNames">
<list>
<value>traceLogAfterMethodThrowExceptionBean</value>
</list>
</property>
</bean>
</beans>
TraceLogMethodThrowsAdviceClient.java
package
com.gaurav.aop.advices.client;
import
org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import
com.gaurav.aop.services.UserTransaction;
public class TraceLogMethodThrowsAdviceClient{
public static
void main(String[] args) {
ApplicationContext appContext = new
ClassPathXmlApplicationContext(
new String[] {
"spring-user-transaction-beans-afterthrows.xml" });
UserTransaction userTransaction = (UserTransaction)
appContext
.getBean("userTransactionProxy");
System.out
.println(" #####
InternetUser ID in client application is:- "
+ userTransaction.getTransactionId() + "\n");
System.out
.println(" #####
InternetUser Name in client application is:- "
+ userTransaction.getUserName() + "\n");
System.out
.println(" #####
Product tried to purchase by InternetUser in client application is :-
"
+ userTransaction.getProductPurchased() + "\n");
System.out
.println(" #####
Transaction status in client application is :- "
+ userTransaction.getTransactionStatus() + "\n");
try {
userTransaction.getException();
} catch (Exception e) {
}
}
}
Result:-
No comments:
Post a Comment