1.第一个例子
1)定义接口
ServiceBean.java
public interface ServiceBean {
public void addUser();
public void deleteUser();
public void updateUser();
public void findUser();
}
2)实现接口
MyServiceBean.java
public class MyServiceBean implements ServiceBean {
public void addUser() {
System.out.println("addUser()");
}
public void deleteUser() {
System.out.println("deleteUser()");
}
public void findUser() {
System.out.println("findUser()");
}
public void updateUser() {
System.out.println("updateUser()");
}
}
3)编写spring配置文件beans.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="serviceTarget" class="com.dexter.service.MyServiceBean" />
</beans>
4)请求spring生成servicebean
public class Main {
public static void main(String[] args) {
XmlBeanFactory xb = new XmlBeanFactory(new ClassPathResource("beans.xml"));
ServiceBean sb = (ServiceBean)xb.getBean("service");
sb.addUser();
xb.destroySingletons();
}
}
2.初步spring aop
1)编写一个LogAdvisor类,这个类是实现MethodBeforeAdvisor接口,实现其中的方法,这个方法在主业务方法 被调用前调用。
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class LogAdvisor implements MethodBeforeAdvice {
@Override
public void before(Method m, Object[] args, Object target)
throws Throwable {
System.out.println("[Log] " + target.getClass().getName() + "." + m.getName() + "()");
}
}
2)编写beans.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="serviceTarget" class="com.dexter.service.MyServiceBean" />
<bean id="logAdvisor" class="com.dexter.service.LogAdvisor"></bean>
<!--请求ProxyFactoryBean类生成代理对象-->
<bean id="service"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.dexter.service.ServiceBean</value>
</property>
<property name="target">
<ref local="serviceTarget" />
</property>
<property name="interceptorNames">
<list>
<value>logAdvisor</value>
</list>
</property>
</bean>
</beans>
3)调用
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
public static void main(String[] args) {
XmlBeanFactory xb = new XmlBeanFactory(new ClassPathResource("beans.xml"));
ServiceBean sb = (ServiceBean)xb.getBean("service");
sb.addUser();
xb.destroySingletons();
}
}
3.编写方法过滤器,编写MethodAdvisor类
1) 编写MethodAdvisor.java
public class MethodAdvisor implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation m) throws Throwable {
m.proceed();
System.out.println("MethodAdvisor invoked,"+m.getMethod().getName());
return "MethodAdvisor invoked";
}
}
2)写配置文件,请求RegexpMethodPointcutAdvisor类过滤指定的方法
<bean id="methodAdvisorTarget"
class="com.dexter.service.MethodAdvisor">
</bean>
<bean id="methodAdvisor"
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="methodAdvisorTarget" />
</property>
<property name="patterns">
<list>
<!--过滤addUser这个指定的方法-->
<value>.*addUser</value>
</list>
</property>
</bean>
<bean id="service"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.dexter.service.ServiceBean</value>
</property>
<property name="target">
<ref local="serviceTarget" />
</property>
<property name="interceptorNames">
<list>
<value>logAdvisor</value>
<value>methodAdvisor</value>
</list>
</property>
</bean>
4.异常aop
1)编写ExceptionAdvisor类
import org.springframework.aop.ThrowsAdvice;
public class ExceptionAdvisor implements ThrowsAdvice {
public void afterThrowing(RuntimeException re) throws Throwable {
System.out.println("[Exception] " + re.getMessage());
}
}
2)配置beans.xml
<bean id="service"
class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<value>com.dexter.service.ServiceBean</value>
</property>
<property name="target">
<ref local="serviceTarget" />
</property>
<property name="interceptorNames">
<list>
<value>logAdvisor</value>
<value>passwordAdvisor</value>
<value>exceptionAdvisor</value>
</list>
</property>
</bean>
3)如果在业务方法执行时候有异常,那么ExceptionAdvisor的afterThrowing方法被执行