----
------------------------------------- Proxy -----------------------------------
[Html]
Import javax. sound. midi. SysexMessage;
Import org. aspectj. lang. ProceedingJoinPoint;
Import org. aspectj. lang. annotation. After;
Import org. aspectj. lang. annotation. AfterReturning;
Import org. aspectj. lang. annotation. AfterThrowing;
Import org. aspectj. lang. annotation. Around;
Import org. aspectj. lang. annotation. Aspect;
Import org. aspectj. lang. annotation. Before;
Import org. aspectj. lang. annotation. Pointcut;
@ Aspect
Public class AgentProxy {
@ Pointcut ("execution (* proxy. spring. agent. annotation .*.*(..))")
Public void sing2 (){
}
@ Before ("sing2 ()")
Public void before (){
System. out. println ("before ()........");
}
@ After ("sing2 ()")
Public void after (){
System. out. println ("after ()......");
}
@ AfterThrowing ("sing2 ()")
Public void afterThrow (){
System. out. println ("after ()......");
}
@ Around ("sing2 ()")
Public void around (ProceedingJoinPoint p ){
// Obtain the Parameter
Object [] o = p. getArgs ();
For (int I = 0; I <o. length; I ++ ){
Object object = o [I];
System. out. println (object );
}
Try {
// This parameter must be added. If this parameter is not added, the original function is not executed.
P. proceed ();
} Catch (Throwable e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
System. out. println ("around ()....");
}
@ AfterReturning ("sing2 ()")
Public void afterReturn (){
System. out. println ("afterReturn ()......");
}
}
Import javax. sound. midi. SysexMessage;
Import org. aspectj. lang. ProceedingJoinPoint;
Import org. aspectj. lang. annotation. After;
Import org. aspectj. lang. annotation. AfterReturning;
Import org. aspectj. lang. annotation. AfterThrowing;
Import org. aspectj. lang. annotation. Around;
Import org. aspectj. lang. annotation. Aspect;
Import org. aspectj. lang. annotation. Before;
Import org. aspectj. lang. annotation. Pointcut;
@ Aspect
Public class AgentProxy {
@ Pointcut ("execution (* proxy. spring. agent. annotation .*.*(..))")
Public void sing2 (){
}
@ Before ("sing2 ()")
Public void before (){
System. out. println ("before ()........");
}
@ After ("sing2 ()")
Public void after (){
System. out. println ("after ()......");
}
@ AfterThrowing ("sing2 ()")
Public void afterThrow (){
System. out. println ("after ()......");
}
@ Around ("sing2 ()")
Public void around (ProceedingJoinPoint p ){
// Obtain the Parameter
Object [] o = p. getArgs ();
For (int I = 0; I <o. length; I ++ ){
Object object = o [I];
System. out. println (object );
}
Try {
// This parameter must be added. If this parameter is not added, the original function is not executed.
P. proceed ();
} Catch (Throwable e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
System. out. println ("around ()....");
}
@ AfterReturning ("sing2 ()")
Public void afterReturn (){
System. out. println ("afterReturn ()......");
}
}
---------------------------------- The proxy class --------------------------------------
[Java]
Import org. aspectj. lang. annotation. Pointcut;
Public class MySinger {
Public void sing2 (){
System. out. println ("I sing ");
}
Public String ss2 (String name ){
System. out. println ("MySinger" + "..." + name );
Return name;
}
}
Import org. aspectj. lang. annotation. Pointcut;
Public class MySinger {
Public void sing2 (){
System. out. println ("I sing ");
}
Public String ss2 (String name ){
System. out. println ("MySinger" + "..." + name );
Return name;
}
}
------------------------------------ Add a sentence in xml ---------------------
<Aop: aspectj-autoproxy> </aop: aspectj-autoproxy>
------------------------------------ Test -----------------------
[Java]
Import org. springframework. context. ApplicationContext;
Import org. springframework. context. support. ClassPathXmlApplicationContext;
Public class Test {
Public static void main (String [] args ){
ApplicationContext app =
New ClassPathXmlApplicationContext ("proxy/spring/agent/annotation/application. xml ");
MySinger singer = (MySinger) app. getBean ("singer ");
Singer. sing2 ();
System. out. println ("main" + "..." + singer. ss2 ("Jay Chou "));
}
}
Import org. springframework. context. ApplicationContext;
Import org. springframework. context. support. ClassPathXmlApplicationContext;
Public class Test {
Public static void main (String [] args ){
ApplicationContext app =
New ClassPathXmlApplicationContext ("proxy/spring/agent/annotation/application. xml ");
MySinger singer = (MySinger) app. getBean ("singer ");
Singer. sing2 ();
System. out. println ("main" + "..." + singer. ss2 ("Jay Chou "));
}
}