Person
package org.zbq.aop;
public interface Person {
public void run();
public void info();
}
PersonImpl
package org.zbq.aop;
public class PersonImpl implements Person {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void info() {
System.out.println("My name is " + name + " I am " + age);
}
@Override
public void run() {
System.out.print("I am " + age + ",");
if(age < 35){
System.out.println("I am young");
}else {
System.out.println("I am Old");
}
}
}
MyBeforAdvisor
package org.zbq.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class MyBeforAdvisor implements MethodBeforeAdvice {
@Override
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
System.out.println("方法调用之前……");
System.out.println("方法是:" + arg0);
System.out.println("参数是:" + arg1);
System.out.println("对象是:" + arg2);
}
}
MyAfterAdvisor
package org.zbq.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class MyAfterAdvisor implements AfterReturningAdvice {
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("方法调用之前……");
System.out.println("方法是:" + arg1);
System.out.println("参数是:" + arg2);
System.out.println("对象是:" + arg3);
}
}
MyAroundInterceptor
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class MyAroundInterceptor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
return invocation.proceed();
}
}
package org.zbq.aop;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Client {
public static void main(String[] args) {
ClassPathResource cpr = new ClassPathResource("applicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory(cpr);
Person p = (Person) factory.getBean("person");
p.info();
p.run();
}
}
applicationContext.xml
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="personTarget" class="org.zbq.aop.PersonImpl">
<property name="name">
<value>Tom</value>
</property>
<property name="age">
<value>51</value>
</property>
</bean>
<bean id="myAdvisor" class="org.zbq.aop.MyBeforAdvisor"/>
<bean id="myAroundInterceptor" class="org.zbq.aop.MyAroundInterceptor"/>
<bean id="runAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<!-- advice属性确定处理bean -->
<property name="advice">
<!-- 此处的处理bean定义采用嵌套bean,也可以引用容器的另一个bean -->
<bean class="org.zbq.aop.MyAfterAdvisor"/>
</property>
<!-- patterns确定正则表达式模式 -->
<property name="patterns">
<list>
<!-- 确定正则表达式列表 -->
<value>.*run.*</value>
</list>
</property>
</bean>
<bean id="person" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>org.zbq.aop.Person</value>
</property>
<property name="target">
<ref local="personTarget"/>
</property>
<property name="interceptorNames">
<list>
<value>runAdvisor</value>
<value>myAdvisor</value>
<value>myAroundInterceptor</value>
</list>
</property>
</bean>
</beans>