import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import aop.cust.service.CustomerService;
public class TestMain {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("aop/config/aop.xml");
CustomerService cs = (CustomerService) ctx.getBean("customerService");
System.currentTimeMillis();//시간재기
try{
cs.registerCustomer();
System.out.println("-------------등록끝----------------");
cs.modifyCustomer();
System.out.println("-------------수정끝----------------");
}catch (Exception e) {
e.printStackTrace();
}
}
}
<?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.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="customerDAO" class="aop.cust.dao.CustomerDAOImpl"/>
<bean id="customerService" class="aop.cust.service.CustomerServiceImpl">
<property name="DAO" ref="customerDAO"/>
</bean>
<!-- Advice등록 -->
<bean id="around" class="aop.cust.advice.AroundAdvice"/>
<!-- aop설정 -->
<aop:config>
<!-- 내 방법 -->
<!--<aop:aspect id="timeCheckAS" ref="around"> -->
<!-- <aop:around method="timeCheckAdvice" pointcut="bean(customer*)"/> -->
<!-- <aop:around method="timeCheckAdvice" pointcut="execution(public * aop..Customer*.*(..))"/>
</aop:aspect>
-->
<!-- 강사님 방법 -->
<aop:pointcut expression="within(aop.cust.service.*ServiceImpl)" id="custServicePC"/>
<aop:pointcut expression="within(aop.cust.dao.*DAOImpl)" id="custDAOPC"/>
<aop:aspect id="timeCheckAS" ref="around">
<aop:around method="timeCheckAdvice" pointcut-ref="custServicePC"/>
<aop:around method="timeCheckAdvice" pointcut-ref="custDAOPC"/>
</aop:aspect>
</aop:config>
</beans>
package aop.cust.advice;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.util.StopWatch;
public class AroundAdvice {
/*
* CustomerDAOImpl.selectCustomerById : 500ms
* CustomerDAOImpl.insertCustomer : 370ms
* CustomerServiceImple.registerCustomer : 870ms
* DAO와 Service 메소드들이 실행한 시간을 로그로 남긴다.
* Target Class.method : 걸린시간
*/
public Object timeCheckAdvice(ProceedingJoinPoint jp)throws Throwable{
// StopWatch sw = new StopWatch();
// sw.start();
// Object retvalue = jp.proceed();
// sw.stop();
// long time = sw.getTotalTimeMillis();
// System.out.println(jp.getTarget().getClass().getName()+"."+jp.getSignature().getName()+"() : "+time+"ms");
// return retvalue;
long endTime = 0;
long startTime = 0;
try{
startTime = System.currentTimeMillis();
Object retValue = jp.proceed();//클라이언트가 요청한 메소드를 실제 호출
System.out.print(jp.getTarget().getClass().getName()+"."+jp.getSignature().getName()+"()");
return retValue;
}catch (Throwable ta) {
throw ta;
}finally{
endTime = System.currentTimeMillis();
System.out.println(" : "+(endTime-startTime)+"ms");
}
}
}
package aop.cust.dao;
public interface CustomerDAO {
public abstract void insertCustomer() throws Exception;
public abstract void updateCustomer() throws Exception;
public abstract void selectCustomerById() throws Exception;
}
package aop.cust.dao;
public class CustomerDAOImpl implements CustomerDAO {
@Override
public void insertCustomer() throws Exception{
Thread.sleep(380);
}
@Override
public void updateCustomer() throws Exception{
Thread.sleep(520);
}
@Override
public void selectCustomerById() throws Exception{
Thread.sleep(620);
}
}
package aop.cust.service;
import aop.cust.dao.CustomerDAO;
public interface CustomerService {
public abstract void setDAO(CustomerDAO dao);
public abstract void registerCustomer() throws Exception;
public abstract void modifyCustomer() throws Exception;
}
package aop.cust.service;
import aop.cust.dao.CustomerDAO;
public class CustomerServiceImpl implements CustomerService {
private CustomerDAO dao;
@Override
public void setDAO(CustomerDAO dao){
this.dao = dao;
}
@Override
public void registerCustomer() throws Exception{
dao.selectCustomerById();
dao.insertCustomer();
}
@Override
public void modifyCustomer() throws Exception{
dao.selectCustomerById();
dao.updateCustomer();
}
}
* 결과
aop.cust.dao.CustomerDAOImpl.selectCustomerById() : 624ms
aop.cust.dao.CustomerDAOImpl.insertCustomer() : 391ms
aop.cust.service.CustomerServiceImpl.registerCustomer() : 1015ms
-------------등록끝----------------
aop.cust.dao.CustomerDAOImpl.selectCustomerById() : 624ms
aop.cust.dao.CustomerDAOImpl.updateCustomer() : 532ms
aop.cust.service.CustomerServiceImpl.modifyCustomer() : 1156ms
-------------수정끝----------------