SPRING

[SPRING] AOP 실습

worri-pi 2021. 8. 25. 17:47
package com.springbook.biz.board.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.springbook.biz.board.BoardService;
import com.springbook.biz.board.BoardVO;
import com.springbook.biz.common.BeforeAdvice;
import com.springbook.biz.common.Log4jAdvice;
import com.springbook.biz.common.LogAdvice;

@Service("boardservice")
public class BoardServiceImpl implements BoardService {
	
	@Autowired  
	private BoardDAO boardDAO;
	
	@Override
	public void insertBoard(BoardVO vo) {
		boardDAO.insertBoard(vo);
	}

	@Override
	public void update(BoardVO vo) {
		boardDAO.update(vo);
	}

	@Override
	public void delete(BoardVO vo) {
		boardDAO.delete(vo);
	}

	@Override
	public BoardVO get(BoardVO vo) {
		return boardDAO.get(vo);
	}

	@Override
	public List<BoardVO> getList(BoardVO vo) {
		return boardDAO.getList(vo);
	}

}
<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
	<context:component-scan base-package="com.springbook.biz"></context:component-scan>

	<bean id="before" class="com.springbook.biz.common.BeforeAdvice"></bean>

	<aop:config>
		<aop:pointcut expression="execution(* com.springbook.biz..*Impl.*(..))" id="allPointcut"/>
		<aop:aspect ref="before">
			<aop:before method="before" pointcut-ref="allPointcut"/>
		</aop:aspect>
	</aop:config>
	 
</beans>
package com.springbook.biz.common;

public class BeforeAdvice {
	public void before() {
		System.out.println("[사전처리] 비지니스 로직 수행 전 동작");
		
	}
}

 

 

 


package com.springbook.biz.board.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.springbook.biz.board.BoardService;
import com.springbook.biz.board.BoardVO;
import com.springbook.biz.common.BeforeAdvice;
import com.springbook.biz.common.Log4jAdvice;
import com.springbook.biz.common.LogAdvice;

@Service("boardservice")
public class BoardServiceImpl implements BoardService {
	
	@Autowired  
	private BoardDAO boardDAO;
	
	
	@Override
	public void insertBoard(BoardVO vo) {
		boardDAO.insertBoard(vo);
	}

	@Override
	public void update(BoardVO vo) {
		boardDAO.update(vo);		
	}

	@Override
	public void delete(BoardVO vo) {
		boardDAO.delete(vo);		
	}

	@Override
	public BoardVO get(BoardVO vo) {
		return boardDAO.get(vo);
	}

	@Override
	public List<BoardVO> getList(BoardVO vo) {
		return boardDAO.getList(vo);
	}

}
<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
	<context:component-scan base-package="com.springbook.biz"></context:component-scan>


	 <bean id="log" class="com.springbook.biz.common.Log4jAdvice"></bean> 
	 
	 


	 <aop:config>
		<aop:pointcut expression="execution(* com.springbook.biz..*Impl.*(..))" id="allPointcut"/>
		<aop:pointcut expression="execution(* com.springbook.biz..*Impl.*get*(..))" id="getPointcut"/>
		<aop:aspect ref="log">
			<aop:before method="printLogging" pointcut-ref="allPointcut"/>
			
		</aop:aspect>
	</aop:config>

	 
</beans>

 

package com.springbook.biz.common;

public class Log4jAdvice {
	public void printLogging() {
		System.out.println("[공통 로그-Log4j] 비즈니스 로직 수행 전 동작");
	}
}


package com.springbook.biz.common;

public class AfterReturningAdvice {
	public void afterLog() {
		System.out.println("[사후처리] 비즈니스 로직 수행 후 동작");
	}
}
<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
	<context:component-scan base-package="com.springbook.biz"></context:component-scan>

<bean id="afterReturning" class="com.springbook.biz.common.AfterReturningAdvice"></bean>

	 <aop:config>
	 	<aop:pointcut expression="execution(* com.springbook.biz..*Impl.get*(..))" id="allPointcut"/>
	 	<aop:aspect ref="afterReturning">
	 		<aop:after method="afterLog" pointcut-ref="allPointcut"/>
	 	</aop:aspect>
	 </aop:config>
	 

	 
</beans>

package com.springbook.biz.common;

public class AfterThrowingAdvice {
	public void exceptionLog() {
		System.out.println("[예외처리]비즈니스 로직 중 예외 발생");
	}
}
<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
	<context:component-scan base-package="com.springbook.biz"></context:component-scan>


	<bean id="afterThrowing" class="com.springbook.biz.common.AfterThrowingAdvice"></bean>
	
	 <aop:config>
	 	<aop:pointcut expression="execution(* com.springbook.biz..*Impl.*(..))" id="allPointcut"/>
	 	<aop:aspect ref="afterThrowing">
	 		<aop:after-throwing method="exceptionLog" pointcut-ref="allPointcut"/>
	 	</aop:aspect>
	 </aop:config>
	 
	 
</beans>
package com.springbook.biz.board.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.springbook.biz.board.BoardService;
import com.springbook.biz.board.BoardVO;
import com.springbook.biz.common.BeforeAdvice;
import com.springbook.biz.common.Log4jAdvice;
import com.springbook.biz.common.LogAdvice;

@Service("boardservice")
public class BoardServiceImpl implements BoardService {
	
	@Autowired  
	private BoardDAO boardDAO;
	
	
	@Override
	public void insertBoard(BoardVO vo) { //test 위해 예외발생시키기
		if(vo.getSeq()==0) {
			throw new IllegalArgumentException("0번 글을 등록할 수 없습니다.");
		}
		boardDAO.insertBoard(vo);
	}
	


	@Override
	public void update(BoardVO vo) {
		boardDAO.update(vo);
		
	}

	@Override
	public void delete(BoardVO vo) {
		boardDAO.delete(vo);
		
	}

	@Override
	public BoardVO get(BoardVO vo) {
		return boardDAO.get(vo);
	}

	@Override
	public List<BoardVO> getList(BoardVO vo) {
		return boardDAO.getList(vo);
	}

}

package com.springbook.biz.common;

public class AfterAdvice {
	public void finallyLog() {
		System.out.println("[사후처리] 비즈니스 로직 수행 후 무조건 동작");
	}
}
package com.springbook.biz.common;

public class AfterThrowingAdvice {
	public void exceptionLog() {
		System.out.println("[예외처리]비즈니스 로직 중 예외 발생");
	}
}
package com.springbook.biz.board.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.springbook.biz.board.BoardService;
import com.springbook.biz.board.BoardVO;
import com.springbook.biz.common.BeforeAdvice;
import com.springbook.biz.common.Log4jAdvice;
import com.springbook.biz.common.LogAdvice;

@Service("boardservice")
public class BoardServiceImpl implements BoardService {
	
	@Autowired  
	private BoardDAO boardDAO;
	
	
	@Override
	public void insertBoard(BoardVO vo) { //test 위해 예외발생시키기
		if(vo.getSeq()==0) {
			throw new IllegalArgumentException("0번 글을 등록할 수 없습니다.");
		}
		boardDAO.insertBoard(vo);
	}
	


	@Override
	public void update(BoardVO vo) {
		boardDAO.update(vo);
		
	}

	@Override
	public void delete(BoardVO vo) {
		boardDAO.delete(vo);
		
	}

	@Override
	public BoardVO get(BoardVO vo) {
		return boardDAO.get(vo);
	}

	@Override
	public List<BoardVO> getList(BoardVO vo) {
		return boardDAO.getList(vo);
	}

}
<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
	<context:component-scan base-package="com.springbook.biz"></context:component-scan>

	<bean id="after" class="com.springbook.biz.common.AfterAdvice"></bean>
	<bean id="afterThrowing" class="com.springbook.biz.common.AfterThrowingAdvice"></bean>
	 
	 <aop:config>
	 	<aop:pointcut expression="execution(* com.springbook.biz..*Impl.*(..))" id="allPointcut"/>
	 	<aop:aspect ref="afterThrowing">
	 		<aop:after-throwing method="exceptionLog" pointcut-ref="allPointcut"/>
	 	</aop:aspect>
	 	<aop:aspect ref="after">
	 		<aop:after-throwing method="finallyLog" pointcut-ref="allPointcut"/>
	 	</aop:aspect>
	 </aop:config>
	 
	

	 
</beans>

 

 

package com.springbook.biz.common;

import org.aspectj.lang.ProceedingJoinPoint;

public class AroundAdvice {
	public Object aroundLog(ProceedingJoinPoint pjp)throws Throwable{
		System.out.println("[BEFORE]: 비즈니스 메소드 수행 전에 처리할 내용..");
		Object returnObj = pjp.proceed();
		System.out.println("[AFTER]: 비즈니스 메소드 수행 후에 처리할 내용..");
		return returnObj;
	}
}
<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
	<context:component-scan base-package="com.springbook.biz"></context:component-scan>

	<bean id="around" class="com.springbook.biz.common.AroundAdvice"></bean>

	 <aop:config>
	 	<aop:pointcut expression="execution(* com.springbook.biz..*Impl.*(..))" id="allPointcut"/>
	 	<aop:aspect ref="around">
	 		<aop:around method="aroundLog" pointcut-ref="allPointcut"></aop:around>
	 	</aop:aspect>
	 </aop:config>
	 
</beans>

 

 

 

728x90