Spring aop annotation Demo

Source: Internet
Author: User

----

------------------------------------- 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 "));
}

}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.