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