AOP[실습] - spring_aop_shop_stu
package shop.common;
import org.aspectj.lang.JoinPoint;
import shop.service.InsufficientInventoryException;
public class OrderAspect {
//꽃이나 책과 관련된 business로직 처리 도중 오류 발생시 호출된 advice메소드 작성
//재고 부족시 - 꽃 재고 부족 : 주문량 - 15
public void orderLogger(Exception ex){
//주문시 오류처리
System.out.println("주문처리시 오류발생");
System.out.print(ex.getMessage());
if(ex instanceof InsufficientInventoryException){
InsufficientInventoryException ie = (InsufficientInventoryException)ex;
System.out.println(" : "+ie.getOrderCount());
}
}
}
package shop.data;
public class WareHouse {
private static WareHouse instance=new WareHouse();
private int flowerCount=10;
private int bookCount=10;
private WareHouse(){}
public static WareHouse getInstance(){
return instance;
}
public int getFlowerCount() {
return flowerCount;
}
public int getBookCount() {
return bookCount;
}
}
package shop.service;
import shop.data.WareHouse;
public class BookServiceImpl implements StoreService {
private WareHouse wareHouse;
public BookServiceImpl() {
}
public BookServiceImpl(WareHouse wareHouse){
this.wareHouse=wareHouse;
}
@Override
public void sell(int count) throws InsufficientInventoryException{
if(wareHouse.getBookCount()<count){
throw new InsufficientInventoryException("Book 재고가 부족!",count);
}
System.out.println("책 "+count+"권 판매 ok!");
}
}
package shop.service;
import shop.data.WareHouse;
public class FlowerServiceImpl implements StoreService {
private WareHouse wareHouse;
public FlowerServiceImpl(WareHouse wareHouse) {
super();
this.wareHouse = wareHouse;
}
@Override
public void sell(int count) throws InsufficientInventoryException {
if(wareHouse.getFlowerCount()<count){
throw new InsufficientInventoryException("꽃 재고 부족",count);
}
System.out.println("꽃 "+count+" 송이 판매!");
}
}
package shop.service;
//재고 부족시 발생시킬 Exception객체
public class InsufficientInventoryException extends Exception {
private int orderCount;
public InsufficientInventoryException() {
}
public InsufficientInventoryException(String message, int orderCount) {
super(message);
this.orderCount = orderCount;
}
public int getOrderCount(){
return orderCount;
}
}
package shop.service;
public interface StoreService {
public abstract void sell(int count) throws InsufficientInventoryException;
}
package shop.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import shop.service.InsufficientInventoryException;
import shop.service.StoreService;
public class Test {
public static void main(String[] args) {
ApplicationContext context
= new ClassPathXmlApplicationContext("shop.xml");
StoreService fs=(StoreService)context.getBean("flower");
StoreService bs=(StoreService)context.getBean("book");
try {
fs.sell(80);
} catch (InsufficientInventoryException e) {
System.out.println(e.getMessage());
}
try {
bs.sell(1);
} catch (InsufficientInventoryException e) {
System.out.println(e.getMessage());
}
}
}
//로그메세지 : 주문량 - 주문개수
//꽃 재고 부족 : 주문량 - 15
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="wh" class="shop.data.WareHouse" factory-method="getInstance"/><!-- 싱글턴 패턴으로 작성이 되어 있어서 factory-method="getInstance"를 사용-->
<bean name="book" class="shop.service.BookServiceImpl">
<constructor-arg ref="wh"/>
</bean>
<bean name="flower" class="shop.service.FlowerServiceImpl">
<constructor-arg ref="wh"/>
</bean>
<!-- 공통로직 처리 bean -->
<bean name="orderAspect" class="shop.common.OrderAspect"/>
<aop:config>
<aop:aspect id="logger" ref="orderAspect">
<aop:after-throwing method="orderLogger" pointcut="execution(public * shop..*Service.sell(..))" throwing="ex"/>
</aop:aspect>
</aop:config>
</beans>
* 결과
주문처리시 오류발생
꽃 재고 부족 : 80
꽃 재고 부족
책 1권 판매 ok!