接口:

1
2
3
4
5
6
package  spring.aop.aspectJ;
public  interface  FunctionServer {
     void  creatdDoc( int  count);
                           
     void  removeDoc( int  count);
}


接口实现类:

1
2
3
4
5
6
7
8
9
10
11
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+ "对象。。。。。。。" );
     }
}

增强类:

1
2
3
4
5
6
7
8
9
10
11
12
13
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

1
2
3
4
<!--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>

测试类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 );
           
     }
}