After Advice (After Returning Advice)
- Both (After Advice and After Returning Advice) are almost same and similar to before advice; only difference is that these services are applied after completion of our business logic.
- We should implement AfterReturningAdvice interface and override afterReturning(Object returnVal, Method method, Object[] arg, Object target) method in order to create AfterReturningAdvice implementation class.
- First parameter contains the return value of the business method or null, rest of the parameters is similar to before Advice method parameters.
Syntax Of AfterReturningAdvice — afterReturning()Method
public void afterReturning(ObjectreturnVal, Method method, Object[] arg,
Object target) throws Throwable {
}
Steps for creating implementation for AfterreturningAdvice :-
2) Place the below code in that java file.
5) Create a java file TraceLogMethodAfterAdviceClient.java in the package com.gaurav.aop.advices.client inside src folder.Place the below available code in that file.
TraceLogAfterReturnMethodExecution.java
package com.gaurav.aop.advices;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class TraceLogAfterReturnMethodExecutionimplements AfterReturningAdvice {
@Override
public void afterReturning(Object returnVal, Method method, Object[] arg,
Object target) throws Throwable {
System.out
.println("\n TraceLogAfterReturningAdvice:-Entered inside AfterReturningAdvice");
}
}
InternetClientService.java
package com.gaurav.aop.services;
import org.springframework.transaction.IllegalTransactionStateException;
public class InternetClientService {
private int internetUserId;
private String internetUserName;
private String connectionURL;
public void setInternetUserId(int internetUserId) {
this.internetUserId = internetUserId;
}
public void setInternetUserName(String internetUserName) {
this.internetUserName = internetUserName;
}
public void setConnectionURL(String connectionURL) {
this.connectionURL = connectionURL;
}
public void displayInternetUserId() {
System.out.println("Internet User ID : " + this.internetUserId);
}
public void displayInternetUserName() {
System.out.println("Internet User Name : " + this.internetUserName);
}
public void displayConnectionURL() {
System.out.println("Connection URL in Internet is : "
+ this.connectionURL);
}
public void displayThrowException() {
throw new IllegalTransactionStateException(internetUserName);
}
}
spring-internet-client-service-beans-afteradvice.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="internetClientService" class="com.gaurav.aop.services.InternetClientService">
<property name="internetUserId" value="315260" />
<property name="internetUserName" value="Gaurav Kumar" />
<property name="connectionURL" value="http://www.javatechtipssharedbygaurav.com/" />
</bean>
<bean id="traceLogAfterMethodBean" class="com.gaurav.aop.advices.TraceLogAfterReturnMethodExecution" />
<bean id="internetClientServiceProxy"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="internetClientService" />
<property name="interceptorNames">
<list>
<value>traceLogAfterMethodBean</value>
</list>
</property>
</bean>
</beans>
TraceLogMethodAfterAdviceClient.java
package com.gaurav.aop.advices.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.gaurav.aop.services.InternetClientService;
public class TraceLogMethodAfterAdviceClient{
public static void main(String[] args) {
ApplicationContext appContext = new ClassPathXmlApplicationContext(
new String[] { "spring-internet-client-service-beans-afteradvice.xml" });
InternetClientService internetClientService = (InternetClientService) appContext
.getBean("internetClientServiceProxy");
System.out
.println(" \n ##### InternetUser ID in client application is as below ");
internetClientService.displayInternetUserId();
System.out
.println(" \n ##### InternetUser Name in client application is as below ");
internetClientService.displayInternetUserName();
System.out
.println(" \n ##### Connection URL for Internet in client application is as below ");
internetClientService.displayConnectionURL();
try {
internetClientService.displayThrowException();
} catch (Exception e) {
}
}
}
Result:-
No comments:
Post a Comment