package org.aaa;
public interface IBean {
public void hello();
}
package org.aaa;
public class BeanImpl implements IBean {
public void hello(){
System.out.println("hello world!");
//throw new MyException();
}
}
package org.aaa;
public class MyException extends RuntimeException {
public MyException(){
super();
}
public String toString(){
return "MyException!";
}
}
package org.aaa;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) {
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
IBean hello=(IBean)app.getBean("hello");
hello.hello();
}
}
package org.aaa;
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 MAspect {
@Pointcut("execution(* org.aaa.IBean.*(..))")
public void method(){}
@Before("method()")
public void before(){
System.out.println("before");
}
@After("method()")
public void after(){
System.out.println("after");
}
@Around("method()")
public Object around(ProceedingJoinPoint p) throws Throwable{
Object value=p.proceed();
return value;
}
@AfterReturning("method()")
public void afterReturn(){
System.out.println("afterRetruning");
}
@AfterThrowing("method()")
public void exception(){
System.out.println("exception!!!!");
}
}
[此贴子已经被作者于2007-3-29 16:15:09编辑过]