Spring-AOP(前置/後置/環繞)通知的例子

一、包結構

 

二、biz接口

package org.aop.biz;

/**
 * 在線圖書銷售系統業務邏輯接口
 * 
 * @author miao
 */
public interface BookBiz {

	/**
	 * 買書的業務邏輯
	 * 
	 * @param userName
	 * @param bookName
	 * @param price
	 * @return
	 */
	public boolean buy(String userName, String bookName, double price);

	/**
	 * 發表書評的業務邏輯
	 * 
	 * @param userName
	 * @param comments
	 */
	public void comment(String userName, String comments);

}

 

 

三、bizImpl實現類

package org.aop.biz.impl;

import org.aop.biz.BookBiz;

/**
 * 在線圖書銷售系統業務邏輯接口實現類
 * 
 * @author miao
 * 
 */
public class BookBizImpl implements BookBiz {

	/**
	 * 購買圖書
	 */
	@Override
	public boolean buy(String userName, String bookName, double price) {
		System.out.println("------------------------");
		System.out.println("·" + userName + "購買圖書:" + bookName);
		System.out.println("·" + userName + "增加積分:" + (int) (price / 10));
		System.out.println("------------------------");
		return true;
	}

	/**
	 * 發表書評
	 */
	@Override
	public void comment(String userName, String comments) {
		System.out.println("------------------------");
		System.out.println("·" + userName + "發表書評:" + comments);
		System.out.println("------------------------");

	}

}

 

 

四、前置/後置/環繞通知

前置:

package org.aop.log;

import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import org.springframework.aop.MethodBeforeAdvice;

/**
 * 前置通知 輸出每個方法的參數,調用的時間
 * 
 * @author miao
 * 
 */
public class LogAdvice implements MethodBeforeAdvice {

	private DateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh時mm分ss秒sss毫秒");

	/**
	 * @param m 方法本身
	 * @param args 方法的參數
	 * @param target 調用此方法的對象
	 */
	public void before(Method m, Object[] args, Object target) throws Throwable {
		System.out.println("\n[系統日誌][" + sdf.format(new Date()) + "[調用方法:" + m.getName() + "(參數是:"
				+ Arrays.toString(args) + ")調用的對象是:" + target);
	}

}

 

 

後置:

package org.aop.log;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.springframework.aop.AfterReturningAdvice;

/**
 * 後置通知
 * 
 * @author miao
 * 
 */
public class RakeOffAdvice implements AfterReturningAdvice {

	/**
	 * @param returnValue 返回值
	 * @param method 方法本身
	 * @param args 參數數組
	 * @param target 對象本身
	 */
	public void afterReturning(Object returnValue, Method method, Object[] args, Object target)
			throws Throwable {
		// 只有buy方法纔有返利
		if (method.getName().equals("buy")) {
			System.out.println("[銷售返利]["
					+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "]"
					+ args[0] + ":返利5元哦親!");
		}
	}

}

 

 

環繞:

package org.aop.log;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * 環繞通知實例
 * 
 * @author miao
 * 
 */
public class RoundAdvice implements MethodInterceptor {

	/**
	 * MethodInterceptor不但封裝目標方法及其入參數組,還封裝了目標方法所在的實例對象
	 * 通過getArguments()可以獲取目標犯法的入參數組,通過proceed()反射調用目標實例相應的方法
	 */
	public Object invoke(MethodInvocation invocation) throws Throwable {
		if ("buy".equals(invocation.getMethod().getName())) {
			Object[] args = invocation.getArguments();// 目標方法入參
			String userName = (String) args[0];
			System.out.println("你好,歡迎光臨!" + userName);// 運行方法前調用
			boolean obj = (Boolean) invocation.proceed();// 調用方法,obj方法的返回值
			if (obj) {
				System.out.println("謝謝惠顧!");// 目標方法之後運行
			} else {
				System.out.println("購買失敗!");// 目標方法之後運行
			}
			return obj;
		}
		return null;
	}

}

 

 

五、spring的配置文件(即前置/後置/環繞通知的配置)

前置:

<?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-2.5.xsd">

	<!-- 真正的業務實現類 -->
	<bean id="bookBizTarget" class="org.aop.biz.impl.BookBizImpl"/>
	
	<!-- 前置通知類 -->
	<bean id="logAdvice" class="org.aop.log.LogAdvice"/>
	
	<!-- 代理類,開始織入 -->
	<bean id="bookBiz" class="org.springframework.aop.framework.ProxyFactoryBean">
		<!-- 相應的業務接口 -->
		<property name="proxyInterfaces">
			<list>
				<value>org.aop.biz.BookBiz</value>
			</list>
		</property>
		<!-- 切面類 -->
		<property name="interceptorNames">
			<list>
				<value>logAdvice</value>
			</list>
		</property>
		<!-- 代理的實現類 -->
		<property name="targetName">
			<value>bookBizTarget</value>
		</property>
	</bean>
	

</beans>

 

 

後置:

<?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-2.5.xsd">

	<!-- 後置通知類 -->
	<bean id="rakeOffAdvice" class="org.aop.log.RakeOffAdvice" />
	
	<!-- 書籍的業務邏輯實現類 -->
	<bean id="bookBiz" class="org.aop.biz.impl.BookBizImpl"/>

	<!-- 通過ID自動代理工廠類 -->
	<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<property name="beanNames">
			<value>*Biz</value>
		</property>
		<property name="interceptorNames">
			<list>
				<value>rakeOffAdvice</value>
			</list>
		</property>
	</bean>
	
</beans>

 

 

環繞:

<?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-2.5.xsd">

	<!-- 真正的業務實現類 -->
	<bean id="bookBizTarget" class="org.aop.biz.impl.BookBizImpl" />

	<!-- 前置通知類 -->
	<bean id="roundAdvice" class="org.aop.log.RoundAdvice" />

	<!-- 代理類,開始織入 -->
	<bean id="bookBiz" class="org.springframework.aop.framework.ProxyFactoryBean">
		<!-- 相應的業務接口 -->
		<property name="proxyInterfaces">
			<list>
				<value>org.aop.biz.BookBiz</value>
			</list>
		</property>
		<!-- 切面類 -->
		<property name="interceptorNames">
			<list>
				<value>roundAdvice</value>
			</list>
		</property>
		<!-- 代理的實現類 -->
		<property name="target" ref="bookBizTarget" />
	</bean>

</beans>

 

 

七、測試類:

package org.aop.test;

import org.aop.biz.BookBiz;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 測試類
 * 
 * @author miao
 * 
 */
public class AopTest {

	/**
	 * 測試類,調用代理的工廠類
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("aopBefore.xml");
		//ApplicationContext context = new ClassPathXmlApplicationContext("aopRound.xml");
		//ApplicationContext context = new ClassPathXmlApplicationContext("aopAfter.xml");
		// 將代理工廠作爲業務接口的子對象
		BookBiz bookBiz = (BookBiz) context.getBean("bookBiz");
		// 直接調用接口方法
		bookBiz.buy("小夥伴", "暴走漫畫", 100);
		bookBiz.comment("王蜜桃", "不要在意這些細節");
	}
}

 

 

八、demo

 Spring-AOP.zip

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章