日期:2014-05-20 浏览次数:20769 次
public class AopTest { private static final Logger logger = Logger.getRootLogger(); public void methodA() { logger.debug("AopTest.methodA()"); } public void methodB() { logger.debug("AopTest.methodB()"); } public void methodAB() { logger.debug("AopTest.methodAB()"); methodA(); methodB(); } }
public class InterceptorTest { private static final Logger logger = Logger.getRootLogger(); public void startInvoke(JoinPoint joinPoint) { String methodName = String.format("%s.%s", joinPoint.getSignature().getDeclaringTypeName() , joinPoint.getSignature().getName() ); logger.debug("startInvoke ----------------->[" + methodName + "]"); } public void endInvoke(JoinPoint joinPoint) { String methodName = String.format("%s.%s", joinPoint.getSignature().getDeclaringTypeName() , joinPoint.getSignature().getName() ); logger.debug("endInvoke *******************>[" + methodName + "]"); } }
<?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" xmlns:tx ="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd" default-autowire="byName"> <!-- support spring annotation --> <context:annotation-config/> <bean id="aopTest" class="aop.AopTest"/> <bean id="interceptorTest" class="aop.InterceptorTest"/> <aop:config proxy-target-class="true"> <aop:aspect id="testAspect" ref="interceptorTest"> <aop:pointcut id="testPointcut" expression="execution(* aop..*.*(..))" /> <aop:before pointcut-ref="testPointcut" method="startInvoke"/> <aop:after pointcut-ref="testPointcut" method="endInvoke"/> </aop:aspect> </aop:config> </beans>
public class Main { public static final ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); public static void main(String[] args) { AopTest aopTest = (AopTest)ctx.getBean("aopTest"); aopTest.methodAB(); aopTest.methodA(); aopTest.methodB(); } }