Spring 17 AOP
SpringAOP : simple to understand
DefaultAdvisorAutoProxyCreator :
![]() |
![]() |
![]() |
![]() |
![]() |
public interface Customer { public void browse() throws Exception; }
public class CustomerImpl implements Customer{ public void browse() throws Exception{
System.out.println("Browsing the internet"); //throw new Exception("Connection lost");
} }
public class CafeOwner {
public void LogInTime(){ System.out.println("Log the In Time and Name of the Customer"); }
public void LogOutTime(){ System.out.println("Log Out Time"); }
public void issueUsageBill(){ System.out.println("Calculate and Issue Bill"); }
public void cancelBilling(){ System.out.println("Cancel Billing"); }
}
|
public class InternetAdvice implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice, MethodInterceptor { private CafeOwner cafeOwner;
public void setCafeOwner(CafeOwner cafeOwner) { this.cafeOwner = cafeOwner; }
public CafeOwner getCafeOwner() { return cafeOwner; }
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable { this.getCafeOwner().LogInTime();
}
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable { this.getCafeOwner().LogOutTime();
}
public void afterThrowing(Throwable throwable){ this.getCafeOwner().cancelBilling(); }
public Object invoke(MethodInvocation method) throws Throwable { System.out.println("Allocate a system to customer"); method.proceed(); System.out.println("Deallocate the system"); return null; } }
|
public class InternetCafe { public static void main(String[] args) throws Exception{ ApplicationContext ctx = new ClassPathXmlApplicationContext("springAop.xml"); Customer customer = (Customer) ctx.getBean("customerProxy"); customer.browse(); } }
<?xml version ="1.0" encoding ="UTF-8"?> <beans> <bean id ="customerImpl" class ="CustomerImpl"/> <bean id = "cafeOwner" class ="CafeOwner"/> <bean id ="internetAdvice" class ="InternetAdvice"> <property name ="cafeOwner" ref ="cafeOwner"/> </bean>
<bean id ="customerProxy" class ="org.springframework.aop.framework.ProxyFactoryBean"> <property name ="target" ref="customerImpl"/> <property name ="interceptorNames" value="cafeOwnerBeforeAndAfterAdvice" />
<!-- <property name ="proxyInterfaces"> <value>Customer</value> </property> -->
</bean>
<bean id ="cafeOwnerBeforeAndAfterAdvice" class ="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name ="advice" ref="internetAdvice"/> <property name ="pattern" value=".*" /> </bean> </beans> |
http://veerasundar.com/blog/2010/01/spring-aop-example-profiling-method-execution-time-tutorial/
spring-aop-demo :
public interface Business { void doSomeOperation(); }
package com.veerasundar.spring.aop;
public class BusinessImpl implements Business {
public void doSomeOperation() { System.out.println("I do what I do best, i.e sleep."); try { Thread.sleep(2000); } catch (InterruptedException e) { System.out.println("How dare you to wake me up?"); } System.out.println("Done with sleeping."); } }
@Aspect public class BusinessProfiler {
@Pointcut("execution(* com.veerasundar.spring.aop.*.*(..))") public void businessMethods() { }
@Around("businessMethods()") public Object profile(ProceedingJoinPoint pjp) throws Throwable { long start = System.currentTimeMillis(); System.out.println("Going to call the method."); Object output = pjp.proceed(); System.out.println("Method execution completed."); long elapsedTime = System.currentTimeMillis() - start; System.out.println("Method execution time: " + elapsedTime + " milliseconds."); return output; } }
public class SpringAOPDemo {
public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml"); Business bc = (Business) context.getBean("myBusinessClass"); bc.doSomeOperation(); }
<beans>
<aop:aspectj-autoproxy />
<bean id="businessProfiler" class="com.veerasundar.spring.aop.BusinessProfiler" /> <bean id="myBusinessClass" class="com.veerasundar.spring.aop.BusinessImpl" />
Output:
Going to call the method. I do what I do best, i.e sleep. Done with sleeping. Method execution completed. Method execution time: 2001 milliseconds.
|
public class CustomerService { private String name; private String url;
public void setName(String name) { this.name = name; }
public void setUrl(String url) { this.url = url; }
public void printName() { System.out.println("Customer name : " + this.name); }
public void printURL() { System.out.println("Customer website : " + this.url); }
public void printThrowException() { throw new IllegalArgumentException(); } }
@Override public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println("HijackBeforeMethod : Before method hijacked!" + method.getName()); } }
public class HijackAfterMethod implements AfterReturningAdvice { @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println("HijackAfterMethod : After method hijacked!"); } }
public class HijackThrowException implements ThrowsAdvice { public void afterThrowing(IllegalArgumentException e) throws Throwable { System.out.println("HijackThrowException : Throw exception hijacked!"); } }
public class HijackAroundMethod implements MethodInterceptor { @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("Method name : " + methodInvocation.getMethod().getName()); System.out.println("Method arguments : " + Arrays.toString(methodInvocation.getArguments()));
// same with MethodBeforeAdvice System.out.println("HijackAroundMethod : Before method hijacked!");
try { // proceed to original method call Object result = methodInvocation.proceed();
// same with AfterReturningAdvice System.out.println("HijackAroundMethod : Before after hijacked!");
return result;
} catch (IllegalArgumentException e) { // same with ThrowsAdvice System.out.println("HijackAroundMethod : Throw exception hijacked!"); throw e; } } }
public class App { public static void main(String[] args) { ApplicationContext appContext = new ClassPathXmlApplicationContext( new String[] { "Spring-Customer.xml" });
CustomerService cust = (CustomerService) appContext.getBean("customerServiceProxy");
System.out.println("*************************"); cust.printName(); System.out.println("*************************"); cust.printURL(); System.out.println("*************************"); try { cust.printThrowException(); } catch (Exception e) {
}
} }ring-Customer.xml :
<beans>
<bean id="customerService" class="com.mkyong.customer.services.CustomerService"> <property name="name" value="Yong Mook Kim" /> <property name="url" value="http://www.mkyong.com" /> </bean>
<bean id="hijackBeforeMethodBean" class="com.mkyong.aop.HijackBeforeMethod" /> <bean id="hijackAfterMethodBean" class="com.mkyong.aop.HijackAfterMethod" /> <bean id="hijackThrowExceptionBean" class="com.mkyong.aop.HijackThrowException" /> <bean id="hijackAroundMethodBean" class="com.mkyong.aop.HijackAroundMethod" /> <bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="customerService" />
<property name="interceptorNames" > <list> <value>hijackBeforeMethodBean</value> <value>hijackAfterMethodBean</value> <value>hijackThrowExceptionBean</value> <value>hijackAroundMethodBean</value> </list> </property> </bean> </beans> |
|
<bean id="customerServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="customerService" /> <property name="interceptorNames" value="customerAdvisor" /> </bean>
<bean id="customerAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="pointcut" ref="customerPointcut" /> <property name="advice" ref="hijackAroundMethodBeanAdvice" /> </bean>
<bean id="customerPointcut" class="org.springframework.aop.support.NameMatchMethodPointcut"> <property name="mappedName" value="printName" /> </bean>
<bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="mappedName" value="printName" /> <property name="advice" ref="hijackAroundMethodBeanAdvice" /> </bean>> se the RegexpMethodPointcut
<bean id="customerAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="patterns" value=".*URL.*" /> <property name="advice" ref="hijackAroundMethodBeanAdvice" /> </bean>
BProxyFactoryBean
<bean class="org.springframework.aop. framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames" value="*Service" /> <property name="interceptorNames" value="customerAdvisor" /> </bean>
DefaultAdvisorAutoProxyCreator : ProxyFactoryBean nor use BeanNameAutoProxyCreator.
<bean class="org.springframework.aop. framework.autoproxy.DefaultAdvisorAutoProxyCreator" />
<bean id="hijackAroundMethodBeanAdvice" class="com.mkyong.aop.HijackAroundMethod" />
<bean id="customerAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="mappedName" value="printName" /> <property name="advice" ref="hijackAroundMethodBeanAdvice" /> </bean>
|
|