实现类:
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+"对象。。。。。。。");
}
}
增强:
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("方法调用前。。。。。。。");
}
}
或增强:
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:
<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);
}
}
转载于:https://blog.51cto.com/qiangmzsx/1371099