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...

REST Methods

GET ============================================= HTTP GET method is used to **read** (or retrieve) a representation of a resource. According to the design of the HTTP specification, GET requests are used only to read data and not change it. Therefore, when used this way, they are considered safe. That is, they can be called without risk of data modification or corruption. Means calling it once has the same effect as calling it 10 times. Additionally, GET is idempotent which means that making multiple identical requests ends up having the same result as a single request. Donot expose unsafe operations via GET. It should never modify any resources on the server. Example: ------------    GET http://www.example.com/customers/12345/orders POST =================================== -> The POST verb is most-often utilized to **create** new resources. In particular, it's used to create subordinate resources. -> That is, subordinate to some other (e.g. parent) reso...