view plaincopy to clipboardprint? public class WelcomeAdvice implements MethodBeforeAdvice{ public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("Welcome,这是MethodBeforeAdvice的before方法!"); } }
public class WelcomeAdvice implements MethodBeforeAdvice{ public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("Welcome,这是MethodBeforeAdvice的before方法!"); } } view plaincopy to clipboardprint? public class ThankYouAdvice implements AfterReturningAdvice{ public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("Thank you, come again! 这是 AfterReturningAdvice的afterReturning方法!"); } }
public class ThankYouAdvice implements AfterReturningAdvice{ public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("Thank you, come again! 这是 AfterReturningAdvice的afterReturning方法!"); } }view plaincopy to clipboardprint? public interface IAopTest { void test(); } public class AopTest implements IAopTest{ public void test() { System.out.println("Aop Testing......."); } } public interface IAopTest { void test(); }
public class AopTest implements IAopTest{ public void test() { System.out.println("Aop Testing......."); } } view plaincopy to clipboardprint? public class TestResultImpl { private IAopTest aopTest; public void setAopTest(IAopTest aopTest) { this.aopTest = aopTest; } public void test() { aopTest.test(); } } public class TestResultImpl { private IAopTest aopTest;
public void setAopTest(IAopTest aopTest) { this.aopTest = aopTest; }
<bean id="testResult" class="aop.TestResultImpl"> <property name="aopTest" ref="proxyFactoryBean"/> </bean> </beans> view plaincopy to clipboardprint? public class MyTest { @Test public void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"aop/test.xml"}); TestResultImpl aopTest = (TestResultImpl)ctx.getBean("testResult"); aopTest.test(); } }
public class MyTest { @Test public void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] {"aop/test.xml"}); TestResultImpl aopTest = (TestResultImpl)ctx.getBean("testResult");
aopTest.test(); } }
这是测试结果:
Welcome,这是MethodBeforeAdvice的before方法! Aop Testing....... Thank you, come again! 这是AfterReturningAdvice的afterReturning方法!