RealTime Example of AfterReturning Advice:-
====================================
Q.what is the meaning 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....
========
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
================
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;
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;
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();
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 productName = scn.next();
String pr = prop.getProperty(productName);
double price = Double.parseDouble(pr);
products.add(productName);
System.out.println("Product Name:-" + products);
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 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 {
public void afterReturning(Object retValue, Method method, Object[] args,
Object target) throws Throwable {
double computePrice = (double) retValue;
String cardType = (String) args[0];
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");
}
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 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());
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<>();
List<String> product = new ArrayList<>();
double amount = proxy.generateBill("Platinum", product);
System.out.println(amount);
}
System.out.println(amount);
}
}
Comments
Post a Comment