* You can find dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)
Press enter or click to view image in full size
Bank.java
package com.codeFactory.business;/** * @author code.factory * */ public class Bank { private int amount = 1000; private String accNo = "SBI12345"; public int deposit(int amount, String accNo) { if (accNo.equals(this.accNo)) { this.amount += amount; } else { throw new AccnoNotFoundException(); } return this.amount; } @Override public String toString() { return "Bank [amount=" + amount + ", accNo=" + accNo + "]"; } }
LogExceptionService.java
package com.codeFactory.service;import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.aop.ThrowsAdvice;import com.codeFactory.business.Bank;/** * @author code.factory * */ public class LogExceptionService implements ThrowsAdvice { /* ThrowsAdvice will not force to implement any method * It is a Marker Interace */
public void afterThrowing(Exception e) { Log l = LogFactory.getLog(Bank.class); l.info("incase of exception in deposit: " + e); } }
AccnoNotFoundException.java
package com.codeFactory.business;/** * @author code.factory * */ public class AccnoNotFoundException extends RuntimeException { @Override public String toString() { return "AccNo invalid"; } }
Client.java
package com.codeFactory.test;import org.springframework.aop.framework.ProxyFactoryBean;import com.codeFactory.business.Bank; import com.codeFactory.service.LogExceptionService;/** * @author code.factory * */ public class Client { public static void main(String... args) { /* create target */ Bank b = new Bank(); /* create advice */ LogExceptionService les = new LogExceptionService(); /* add target + advice to proxy */ ProxyFactoryBean bean = new ProxyFactoryBean(); bean.setTarget(b); bean.addAdvice(les);
/* get generated proxy object */ Bank bankProxy = (Bank) bean.getObject(); int amt = bankProxy.deposit(150, "SBI12345"); System.out.println(amt);
amt = bankProxy.deposit(150, "SBI1234"); System.out.println(amt);
} }
Output:
1150 Dec 23, 2020 1:19:28 PM com.codeFactory.service.LogExceptionService afterThrowing INFO: incase of exception in deposit: AccNo invalid Exception in thread "main" AccNo invalid at com.codeFactory.business.Bank.deposit(Bank.java:16) at com.codeFactory.business.Bank$$FastClassByCGLIB$$1a9c7cc.invoke(<generated>) ... ... at com.codeFactory.business.Bank$$EnhancerByCGLIB$$99d62c76.deposit(<generated>) at com.codeFactory.test.Client.main(Client.java:28)