Spring AOP Notification Simple example

Source: Internet
Author: User
Tags aop throwable

Package cn.itcast.service;

Import Org.aspectj.lang.ProceedingJoinPoint;
Import Org.aspectj.lang.annotation.Aspect;
Import Org.aspectj.lang.annotation.Before;
Import Org.aspectj.lang.annotation.Pointcut;


@Aspect
public class Myinterceptor {

@Pointcut ("Execution (* cn.itcast.service.impl.personservicebean.* (..))")
private void Anymethod ()
{
SYSTEM.OUT.PRINTLN ("slice-----");
}

@Before ("Anymethod ()")
public void Doaccesscheck ()
{
SYSTEM.OUT.PRINTLN ("predecessor notice");
}

@AfterReturning (pointcut= "Anymethod ()", returning= "result")
public void doafterreturning (String result)
{
System.out.println ("Post notice" +result);
}

public void Doafter ()
{
System.out.println ("Final Notice");
}

public void doafterthrowing ()
{
SYSTEM.OUT.PRINTLN ("exceptional notice");
}

@Around ("Anymethod ()")
Public Object dobasicprofiling (Proceedingjoinpoint pjp) throws Throwable
{
System.out.println ("entry method");
Object result=pjp.proceed ();
System.out.println ("Exit Method");
return result;

}
}


/**

@Aspect
public class Myinterceptor {

@Pointcut ("Execution (* cn.itcast.service.impl.personservicebean.* (..))")
private void Anymethod ()
//{
SYSTEM.OUT.PRINTLN ("slice-----");
//}
@Before ("Anymethod () && args (name)")
public void Doaccesscheck (String name)
{
SYSTEM.OUT.PRINTLN ("predecessor notice" +name);
}

@AfterReturning (pointcut= "Anymethod ()", returning= "result")
public void doafterreturning (String result)
{
System.out.println ("Post notice" +result);
}

public void Doafter ()
{
System.out.println ("Final Notice");
}

public void doafterthrowing ()
{
SYSTEM.OUT.PRINTLN ("exceptional notice");
}

@Around ("Anymethod ()")
Public Object dobasicprofiling (Proceedingjoinpoint pjp) throws Throwable
{
System.out.println ("entry method");
Object result=pjp.proceed ();
System.out.println ("Exit Method");
return result;

}
}


**/

-----------------------------

Package cn.itcast.service;

Import java.util.List;
Import Java.util.Map;
Import java.util.Properties;
Import Java.util.Set;

Public interface Personservice {

public void Save (String name);
public void Update (String name, Integer PersonID);
Public String getpersonname (Integer PersonID);

}

-------------------------------------

Package Cn.itcast.service.impl;

Import Cn.itcast.service.PersonService;

public class Personservicebean implements Personservice {


Public String getpersonname (Integer PersonID) {
SYSTEM.OUT.PRINTLN ("------This is the Getpersonname method");
return "XXX";
}

public void Save (String name) {
throw new RuntimeException ("I love the Exception");
SYSTEM.OUT.PRINTLN ("------This is the Save Method");
}

public void Update (String name, Integer PersonID) {

SYSTEM.OUT.PRINTLN ("------This is the Update method");

}





}

---------------------------------------

<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:context= "Http://www.springframework.org/schema/context"
xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<aop:aspectj-autoproxy/>

<bean id= "Myinterceptor" class= "Cn.itcast.service.MyInterceptor"/>


<bean id= "Personservice" class= "Cn.itcast.service.impl.PersonServiceBean" ></bean>


</beans>

--------------------

Package test;

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;

Import Cn.itcast.service.PersonService;

public class Test {

/**
* @param args
*/
public static void Main (string[] args) {
ApplicationContext cxt = new Classpathxmlapplicationcontext ("App.xml");
Personservice p = (personservice) cxt.getbean ("Personservice");
P.save ("222");
P.getpersonname (222);



}
}

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.