Skip to main content

Q.what is the meaning of AfterReturning Advice: ?

RealTime Example of AfterReturning Advice:-
====================================
Q.what is the meaning of AfterReturning Advice: ?
Simple way after executing my targetClass business logic it return some data which will validate/check to achive flexibility inside my cross cutting logic / aspect class and that will be executed automatically for the purpose of specially validating
Example:-
========
suppose m a regular customer in pantaloon and as per my transaction record pantaloons offer me an payBack card which is for ex golden card ok....now i purchase 80000$ from pantaloon and after billing my product they gave me a printing bill....
Based on my bill Amount and my membership cardType they will decided as per there business benifit policy how much discount offer will give to me which will decided at runtime means after purchase.......
see if they directly announce or display same policy in his shop then which will be completely tightly coupled coz every day discount will not available na..so thats why they decide how much offer will give to which type of membership based on their monthly transaction thats why they completely separete this policy from all customer...so we can say this is my secondary logic coz if they don;t give offer also as i needed that product i will definetly buy so which will not affect to my purchase.......
Same thing i will show below in programmematically...just focous on it.....
product.properties
================
product1=1200
product2=1500
product3=2000
product4=2500
primeryLogic class:-(Calculate bill )
============================
package com.spring.beans;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;
public class BillGenerator {
public double generateBill(String cardType, List<String> products)
throws IOException {
// compute the price for indivisual product
// credit point to the card
// return amount;
double totalPrice = 0;
Properties prop = new Properties();
FileInputStream fis = new FileInputStream(
new File("product.properties"));
prop.load(fis);
Scanner scn = new Scanner(System.in);
System.out.println("Enter No of product want to buy:-");
int no = scn.nextInt();
for (int i = 1; i <= no; i++) {
System.out.println("Enter ur product name");
String productName = scn.next();
String pr = prop.getProperty(productName);
double price = Double.parseDouble(pr);
products.add(productName);
System.out.println("Product Name:-" + products);
totalPrice = totalPrice + price;
}
return totalPrice;
}
}
Aspect class (Secondary logic for give offer):-
====================================
package com.spring.aspect;
import java.lang.reflect.Method;
import java.util.List;
import org.springframework.aop.AfterReturningAdvice;
public class OfferAscept implements AfterReturningAdvice {
@Override
public void afterReturning(Object retValue, Method method, Object[] args,
Object target) throws Throwable {
double computePrice = (double) retValue;
String cardType = (String) args[0];
if (computePrice > 2000 && cardType.equalsIgnoreCase("Platinum")) {
System.out
.println("Congrats!! u will get 20% off in ur next Purchase and ur coupneCardNo is:p20");
} else if (computePrice >= 1500 && cardType.equalsIgnoreCase("Golden")) {
System.out
.println("Congrats!! u will get 15% off in ur next Purchase and ur coupneCardNo is:g15");
} else {
System.out
.println("Congrats!! u will get 5% off in ur next Purchase and ur coupneCardNo is:f5");
}
}
}

TestClass:-
==========
package com.spring.beans;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.springframework.aop.framework.ProxyFactory;
import com.spring.aspect.OfferAscept;
public class Test {
public static void main(String[] args) throws IOException {
ProxyFactory pf = new ProxyFactory();
pf.setTarget(new BillGenerator());
pf.addAdvice(new OfferAscept());
BillGenerator proxy = (BillGenerator) pf.getProxy();
List<String> product = new ArrayList<>();
double amount = proxy.generateBill("Platinum", product);
System.out.println(amount);
}
}

Comments

Popular posts from this blog

Mockito interview Questions

1.       Question 1. What Is Mockito? Answer : Mockito allows creation of mock object for the purpose of Test Driven Development and Behavior Driven development. Unlike creating actual object, Mockito allows creation of fake object (external dependencies) which allows it to give consistent results to a given invocation. 2.       Question 2. Why Do We Need Mockito? What Are The Advantages? Answer : Mockito differentiates itself from the other testing framework by removing the expectation beforehand. So, by doing this, it reduces the coupling. Most of the testing framework works on the "expect-run-verify". Mockito allows it to make it "run-verify" framework. Mockito also provides annotation which allows to reduce the boilerplate code. 3.       Question 3. Can You Explain A Mockito Framework? Answer : In Mockito, you always check a particular class. The dependency in that class is injected using m...

JAVA Expert Interview Questions Answers 2017

Java Basics ::  Interview Questions and Answers Home  »  Interview Questions  »  Technical Interview  »  Java Basics  » Interview Questions 1.     What is the difference between a constructor and a method? A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. 2.     What is the purpose of garbage collection in Java, and when is it used? The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. 3.  ...

Java Example Program to Convert List to Set

import java.util.ArrayList; import java.util.HashSet; import java.util.Set; public class ListToSet {  /**   * @author Amarjit Kumar   * @category interview questions   *   * Description: Convert List to set in java with example program   *   */  public static void main(String[] args) {   ArrayList<String> arrList= new ArrayList<>();   arrList.add("Java");   arrList.add("Java");   arrList.add("List to String");   arrList.add("Example Program");   Set<String> strSet = new HashSet<String>(arrList);   System.out.println(strSet);   System.out.println(arrList);  } } /*  * Java program to convert list to set. Convert ArrayList of string to HashSet  * in java example program How to convert List to Set in java Set<String> strSet  * = new HashSet<String>(arrList); HashSet having a constructor which will take  * list as an ar...