ProxyFactoryBeanTest.java
package com.studyspring.ch3;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
public class ProxyFactoryBeanTest {
public static void main(String[] args) {
ApplicationContext context=new FileSystemXmlApplicationContext("/src/applicationContext.xml");
MultipleMethod target=(MultipleMethod)context.getBean("person");
target.getTitle();
target.getName();
target.getAge();
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="advice" class="com.studyspring.ch3.BeforeAdvicetest"
abstract="false" singleton="true" lazy-init="default"
autowire="default" dependency-check="default">
</bean>
<bean id="advisor"
class="org.springframework.aop.support.DefaultPointcutAdvisor"
abstract="false" singleton="true" lazy-init="default"
autowire="default" dependency-check="default">
<property name="advice">
<ref local="advice" />
</property>
<property name="pointcut">
<bean class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern">
<value>.*get.*</value>
</property>
</bean>
</property>
</bean>
<bean id="person"
class="org.springframework.aop.framework.ProxyFactoryBean"
abstract="false" singleton="true" lazy-init="default"
autowire="default" dependency-check="default">
<property name="target">
<bean class="com.studyspring.ch3.MultipleMethod">
<property name="name"><value>Ray</value></property>
<property name="age"><value>21</value></property>
<property name="title"><value>doctor</value></property>
</bean>
</property>
<property name="interceptorNames">
<list>
<value>advisor</value>
</list>
</property>
</bean></beans>
MultipleMethod.java
package com.studyspring.ch3;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class MultipleMethod {
private Log log=LogFactory.getLog(MultipleMethod.class);
private String name;
private int age;
private String title;
public int getAge() {
log.info("我们处理get方法");
System.out.println("我们处理get方法");
return age;
}
public void setAge(int age) {
log.info("我们处理set方法");
System.out.println("我们处理set方法");
this.age = age;
}
public String getName() {
log.info("我们处理get方法");
System.out.println("我们处理get方法");
return name;
}
public void setName(String name) {
log.info("我们处理set方法");
System.out.println("我们处理set方法");
this.name = name;
}
public String getTitle() {
log.info("我们处理get方法");
System.out.println("我们处理get方法");
return title;
}
public void setTitle(String title) {
log.info("我们处理set方法");
System.out.println("我们处理set方法");
this.title = title;
}
}
BeforeAdvicetest .java
package com.studyspring.ch3;
import java.lang.reflect.Method;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.framework.ProxyFactory;
public class BeforeAdvicetest implements MethodBeforeAdvice{
/**
* @param args
*/
private Log log=LogFactory.getLog(BeforeAdvicetest.class);
public void before(Method method, Object[] arg, Object target) throws Throwable {
log.info("Now we use before advice");
System.out.println("Now we use before advice");
}
}