xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
beforeMethodBean
注意:为了使用proxy(代理)我们需要引入 CGLIB2, pom.xml文件中注入如下:
[html]
cglib
2.2.2
运行测试类:
[java]
package com.myapp.core.aop.advice;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("resource/aop.xml");
Book book = (Book) context.getBean("bookProxy");
System.out.println("---------------------");
book.printName();
System.out.println("---------------------");
book.printUrl();
System.out.println("----------------------");
try{
book.printThrowException();
}catch(Exception e){
// e.printStackTrace();
}
}
}
注意以上获得的是代理bean;
运行结果如下:
[plain]
三月 20, 2013 2:18:56 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@eb67e8: startup date [Wed Mar 20 14:18:55 CST 2013]; root of context hierarchy
三月 20, 2013 2:18:56 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [resource/aop.xml]
三月 20, 2013 2:18:57 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@157985: defining beans [book,beforeMethodBean,bookPro