Spring AOP--Aspect的JDK方式

接口:
java

package spring.aop.aspectJ;
public interface FunctionServer {
    void creatdDoc(int count);
                          
    void removeDoc(int count);
}


接口實現類:spring

package spring.aop.aspectJ;
public class FunctionServerImp implements FunctionServer {
    @Override
    public void creatdDoc(int count) {
        System.out.println("建立了"+count+"對象。。。。。。。");
    }
    @Override
    public void removeDoc(int count) {
        System.out.println("刪除了"+count+"對象。。。。。。。");
    }
}

加強類:app

package spring.aop.aspectJ;
import java.lang.reflect.Method;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.aop.MethodBeforeAdvice;
@Aspect
public class Porformant{
    @Before("execution(* creatdDoc(..))")
    public void before() {
        System.out.println("方法調用前。。。。。。。");
        System.out.println("輸入的參數:"+"..........");
    }
}

applicationContext.xml
ide

<!--proxy-target-class="false"表示使用JDK方式,若爲true表示CGLIB;若是爲false可是沒有提供接口,Spring也會自動選擇CGLib模式-->
<aop:aspectj-autoproxy proxy-target-class="false"></aop:aspectj-autoproxy>
        <bean id="targetFunctionServer" class="spring.aop.aspectJ.FunctionServerImp"></bean>
        <bean id="aspectPorformant" class="spring.aop.aspectJ.Porformant"></bean>

測試類:測試

package spring.aop.aspectJ;
import org.junit.Test;
import org.springframework.aop.aspectj.annotation.AspectJProxyFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AspectJDKTest {
    static ApplicationContext context=null;
    static{
        context=new ClassPathXmlApplicationContext("applicationContext.xml");
    }
    @Test
    public void aspectTest(){
        FunctionServer functionServer=context.getBean("targetFunctionServer",FunctionServer.class);
          
        functionServer.creatdDoc(10);
        functionServer.removeDoc(20);
          
    }
}
相關文章
相關標籤/搜索