直接看代码
- package edu.hust.springProxy;
- //抽象角色
- public interface RoleDAO {
- public void doSomething();
- }
- package edu.hust.springProxy;
- //真实角色
- public class RoleDAOImpl implements RoleDAO {
- public void doSomething() {
- System.out.println("吃牛肉。。。。。");
- }
- }
- package edu.hust.springProxy;
- import java.lang.reflect.Method;
- import org.springframework.aop.MethodBeforeAdvice;
- public class Before implements MethodBeforeAdvice {
- //before方法定义了一个advice的实现
- /*
- * 参数Method method是advice开始后被执行的方法, 方法名称可以用作判断是否执行代码的条件。
- * Object[] args是传给被调用的public方法的参数数组. 当需要记日志时, 参数args和method都是非常有用的信息。
- * Object target是执行方法m对象的引用。
- *
- * Interface:org.springframework.aop.MethodBeforeAdvice
- * 这个接口只有一个方法:void before(Method method, Object[] args, Object target)throws Throwable
- * 方法解释:Callback before a given method is invoked.
- * Parameters:
- * (1)method - method being invoked
- * (2)args - arguments to the method
- * (3)target - target of the method invocation. May be null.
- *
- * */
- public void before(Method method, Object[] args, Object target) throws Throwable {
- System.out.println("做事情之前, 我想我可以做点其他事情:such as --> 判断一下权限..");
- }
- }
- package edu.hust.springProxy;
- import java.lang.reflect.Method;
- import org.springframework.aop.AfterReturningAdvice;
- public class After implements AfterReturningAdvice {
- /*
- * Interface:org.springframework.aop.AfterReturningAdvice
- * 这个接口只有一个方法:void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable
- * 方法解释:Callback after a given method successfully returned.
- * Parameters:
- * (1)returnValue - the value returned by the method, if any
- * (2)method - method being invoked
- * (3)args - arguments to the method
- * (4)target - target of the method invocation. May be null.
- *
- * */
- public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
- System.out.println("做事情之后, 我想我还可以做点其他事情:such as --> 写个日志..");
- }
- }
- package edu.hust.springProxy;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Client {
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
- RoleDAO roleDAO = (RoleDAO) ctx.getBean("proxy");
- roleDAO.doSomething();
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
- <beans>
- <!-- spring中的bean只能对类进行申明, 而不能对接口进行申明. 在某些bean的property属性中需要引入接口的话, 可以使用value来引入 -->
- <bean id="roleDAOImpl" class="edu.hust.springProxy.RoleDAOImpl"></bean>
- <bean id="before" class="edu.hust.springProxy.Before"></bean>
- <bean id="after" class="edu.hust.springProxy.After"></bean>
- <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">
- <!-- 对于引入了ProxyFactoryBean类的bean:
- 其引入"抽象角色"的property标签的name属性值必须为proxyInterfaces;
- 其引入"真实角色"的property标签的name属性值必须为target;
- 其引入"代理角色"的property标签的name属性值必须为interceptorNames;
- 至于为什么, 请看ProxyFactoryBean类的源代码 -->
- <!-- proxyInterfaces:定义proxy要实现哪些接口, 可以是一个, 也可以是多个(多个的话,要用list标签) -->
- <property name="proxyInterfaces">
- <value>edu.hust.springProxy.RoleDAO</value>
- </property>
- <!-- target:定义被代理的对象 -->
- <property name="target" ref="roleDAOImpl"></property>
- <!-- interceptorNames:定义proxy被切入了哪些通知, 可以是一个, 也可以是多个(多个的话,要用list标签) -->
- <property name="interceptorNames">
- <list>
- <value>before</value>
- <value>after</value>
- </list>
- </property>
- </bean>
- </beans>