Spring AOP--Aspect的CGLib方式

實現類:java

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

加強:spring

package spring.aop.aspectJ.cglib;
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("方法調用前。。。。。。。");
    }
}

或加強:app

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(..))||execution(* removeDoc(..))")
    public void before() {
        System.out.println("方法調用前。。。。。。。");
    }
}


applicationContent.xml:ide

<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
        <bean id="targetFunctionServer" class="spring.aop.aspectJ.cglib.FunctionServerImp"></bean>
        <bean id="aspectPorformant" class="spring.aop.aspectJ.cglib.Porformant"></bean>

測試類:測試

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