今天重新看了 動態代理模式,又看了一遍Spring AOP,把我的項目慢慢的添加Spring,把我瞭解的AOP和大家分享一下。
下面的例子參考了別人的代碼
添加Spring3.0 需要的包
定義一個接口 HelloInterface
- package rw.hello;
- public interface HelloInterface {
- void BeforeHello();
- void ExecuteHello();
- void AfterHello();
- }
實現這個接口的類 Hello
- package rw.hello;
- public class Hello implements HelloInterface{
- public void BeforeHello() {
- // TODO Auto-generated method stub
- System.out.println("----------------Hello 方法執行之前");
- }
- public void ExecuteHello() {
- // TODO Auto-generated method stub
- System.out.println("----------------Hello 方法執行中......");
- }
- public void AfterHello() {
- // TODO Auto-generated method stub
- System.out.println("----------------Hello 方法執行之後");
- }
- }
現在想在 Hello 類的
- BeforeHello(),ExecuteHello(),AfterHello()這三個方法執行之前做其它事情,比如權限問題,記錄日誌之類.....反正我今天是添加的用戶權限問題
- 同樣有3個類,風別是BeforeHello(),ExecuteHello(),AfterHello()這三個方法調用之前要調用的
- 下面只測試一下After.java 和 Before.java
- After.java
- package rw.method;
- import org.aspectj.lang.JoinPoint;
- public class After {
- public void invoke(JoinPoint joinpoint){
- System.out.println("After 類"+joinpoint.getSignature().getName());
- }
- }
- Before.java
- package rw.method;
- import org.aspectj.lang.JoinPoint;
- public class Before {
- public void invoke(JoinPoint joinpoint){
- System.out.println("Before 類"+joinpoint.getSignature().getName());
- }
- }
- <?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:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springsource.org/schema/aop/spring-aop-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
- <bean id="hello" class="rw.hello.Hello"/>
- <bean id="after" class="rw.method.After"/>
- <bean id="before" class="rw.method.Before"/>
- <bean id="execute" class="rw.method.Execute"/>
- <aop:config>
- <aop:pointcut expression="execution(* rw.hello.HelloInterface.BeforeHello(..))" id="beforepointcut"/>
- <aop:aspect id="beforeaspect" ref="before">
- <aop:before method="invoke" pointcut-ref="beforepointcut"/>
- </aop:aspect>
- </aop:config>
- <aop:config>
- <aop:pointcut expression="execution(* rw.hello.HelloInterface.AfterHello(..))" id="afterpointcut"/>
- <aop:aspect id="afteraspact" ref="after">
- <aop:after method="invoke" pointcut-ref="afterpointcut"/>
- </aop:aspect>
- </aop:config>
- </beans>
1,在Hello的BeforeHello()方法執行之前 會調用Before類中的invoke(JoinPoint joinpoint)方法
2,在Hello的AfterHello()方法執行之後 會調用After類中的invoke(JoinPoint joinpoint)方法
測試類
- package rw.test;
- import org.junit.Before;
- import org.junit.Test;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import rw.hello.HelloInterface;
- public class AllTest {
- private ApplicationContext context;
- @Before
- public void setUp() throws Exception {
- context=new ClassPathXmlApplicationContext("applicationContext.xml");
- }
- @Test
- public void TestHelloInterface(){
- HelloInterface hello=(HelloInterface) context.getBean("hello");
- hello.BeforeHello();
- hello.AfterHello();
- }
- }
輸出結果
Hello 類中的 void ExecuteHello()方法在applicationContext.xml中沒有加以任何配置
正常輸出