鍍金池/ 教程/ Java/ Spring AOP實例(Pointcut, Advisor)
Spring EL hello world實例
Spring使用@Autowired注解自動裝配
MapFactoryBean實例
Bean InitializingBean和DisposableBean實例
Spring使用@Required注解依賴檢查
安裝Spring工具套件到Eclipse
CGLIB is required to process @Configuration classes
Spring EL Arrays, Lists, Maps實例
Spring由類型(Type)自動裝配
Spring在bean配置文件中定義電子郵件模板
Spring發(fā)送帶附件郵件
ListFactoryBean實例
Struts 2 + Spring + Hibernate集成實例
Spring bean引用實例
Spring JdbcTemplate batchUpdate()實例
Spring SetFactoryBean實例
Spring通過setter方法注入
Spring EL hello world實例
在bean配置文件中的電子郵件模板
Spring發(fā)送帶附件的Email
Spring EL 方法調(diào)用實例
Spring自動裝配Beans
Spring由構(gòu)造方法自動裝配
Spring使用@Required注解依賴檢查
Spring SimpleJdbcTemplate查詢示例
自定義@Required-style注解
Spring+JDBC實例
Spring EL三元運算(if-then-else)實例
JdbcTemplate+JdbcDaoSupport實例
Spring自動掃描組件
Spring EL方法調(diào)用實例
Spring自動裝配Beans
Spring EL bean引用實例
Spring依賴注入(DI)
Spring EL Lists,Maps實例
Spring通過構(gòu)造方法依賴注入
通過MailSender發(fā)送電子郵件
Spring AOP實例(Pointcut, Advisor)
Spring EL運算符實例
Spring JavaConfig實例
Spring依賴注入 (DI)
Spring EL bean引用實例
Spring由AutoDetect自動裝配
Spring由構(gòu)造方法自動裝配
Spring JdbcTemplate查詢實例
Spring SimpleJdbcTemplate batchUpdate()實例
Spring+JDBC實例
如何注入值到Spring bean屬性
Spring SimpleJdbcTemplate類命名參數(shù)實例
Spring PropertyPlaceholderConfigurer實例
Spring JdbcTemplate batchUpdate() 實例
Spring EL與ExpressionParser測試
Spring內(nèi)部Bean實例
加載多個Spring bean配置文件
Spring+Hibernate+MySql實例(注解)
Spring AOP+AspectJ 在XML配置實例
Spring自動代理創(chuàng)建者實例
Spring EL正則表達(dá)式實例
測試 Spring EL與ExpressionParser
java.lang.ClassNotFoundException: org.exolab.castor.xml.XMLExcep
Spring PropertyPlaceholderConfigurer實例
Spring bean配置繼承
Spring使用Setter依賴注入
Spring自動裝配@Qualifier實例
Spring依賴檢查
Spring hello world實例
Spring依賴注入servlet會話監(jiān)聽器
Spring自定義@Required-style注解
Spring bean作用域
Spring AOP攔截器的序列
注入值到Spring Bean的屬性
Spring JdbcTemplate+JdbcDaoSupport實例
Spring教程
Spring自動裝配@Qualifier實例
Spring MapFactoryBean例子
Spring Bean作用域?qū)嵗?/span>
Cannot proxy target class because CGLIB2 is not available
Spring AOP攔截器的順序
Spring集合(List, Set, Map, and Properties)實例
Spring SimpleJdbcTemplate類命名參數(shù)實例
Spring通過自動檢測自動裝配
Spring+Hibernate+MySql實例
Spring JavaConfig @Import實例
訪問MessageSource的bean(MessageSourceAware)
Spring JdbcTemplate查詢實例
Spring JavaConfig @Import實例
Spring Object/XML映射實例
Spring Bean引用例子
Spring AOP+AspectJ注解實例
java.lang.ClassNotFoundException: org.apache.xml.serialize.XMLSe
Spring依賴檢查
Spring JavaConfig實例
Spring AOP+AspectJ注解實例
Spring bean配置繼承
Spring自動掃描組件
自動代理創(chuàng)建者實例
Spring Bean init-method 和 destroy-method實例
Spring通過構(gòu)造方法注入
Spring過濾器組件自動掃描
Spring構(gòu)造方法注入類型歧義
Spring+JDBC實例
Spring EL正則表達(dá)式實例
Spring通過Gmail SMTP服務(wù)器MailSender發(fā)送電子郵件
Spring AOP+AspectJ在XML配置實例
SetFactoryBean實例
Spring注入日期到bean屬性-CustomDateEditor
Spring AOP通知實例 – Advice
Spring使用@Autowired注解自動裝配
Spring AOP實例(Pointcut,Advisor)
Spring AOP在Hibernate的事務(wù)管理
Spring初學(xué)快速入門
Spring內(nèi)部bean實例
Spring bean加載多個配置文件
Spring + Hibernate+ MySql注解實例
Spring @PostConstruct和@PreDestroy實例
Spring依賴注入servlet會話監(jiān)聽器
Spring ListFactoryBean實例
Spring松耦合的實例
Spring過濾器組件自動掃描
Spring按名稱(Name)自動裝配
Spring AOP通知實例(Advice)
Spring AOP在Hibernate事務(wù)管理
Bean init-method和destroy-method實例
Spring EL操作符實例
Spring注入日期到bean屬性(CustomDateEditor)
資源加載程序的getResource()示例
Bean @PostConstruct和@PreDestroy實例
Struts2+Spring集成實例
Spring集合 (List,Set,Map,Properties) 實例
Spring按類型(Type)自動裝配
Spring由名稱(Name)自動裝配
Spring SimpleJdbcTemplate batchUpdate()實例
Spring hello world實例
Spring Bean InitializingBean和DisposableBean實例
Spring構(gòu)造方法注入類型歧義
Spring EL三元操作符(if-then-else)實例
Spring+Hibernate+MySql實例
Spring松耦合實例

Spring AOP實例(Pointcut, Advisor)

在上一個Spring AOP通知的例子,一個類的整個方法被自動攔截。但在大多數(shù)情況下,可能只需要一種方式來攔截一個或兩個方法,這就是為什么引入'切入點'的原因。它允許你通過它的方法名來攔截方法。另外,一個“切入點”必須具有“Advisor' 相關(guān)聯(lián)。
在Spring AOP中,有三個非常專業(yè)術(shù)語- Advices, Yiibaicut , Advisor,把它在非官方的方式...
  • Advice – 指示之前或方法執(zhí)行后采取的行動。
  • Yiibaicut – 指明哪些方法應(yīng)該攔截,通過方法的名稱或正則表達(dá)式模式。
  • Advisor – 分組"通知"和”切入點“成為一個單元,并把它傳遞到代理工廠對象。

再次回顧上一個 Spring AOP通知的例子。

File : CustomerService.java

package com.yiibai.customer.services;

public class CustomerService
{
	private String name;
	private String url;

	public void setName(String name) {
		this.name = name;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void printName(){
		System.out.println("Customer name : " + this.name);
	}
	
	public void printURL(){
		System.out.println("Customer website : " + this.url);
	}
	
	public void printThrowException(){
		throw new IllegalArgumentException();
	}
	
}

File : applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="customerService" class="com.yiibai.customer.services.CustomerService">
		<property name="name" value="Yong Mook Kim" />
		<property name="url" value="http://www.yiibai.com" />
	</bean>

	<bean id="hijackAroundMethodBeanAdvice" class="com.yiibai.aop.HijackAroundMethod" />

	<bean id="customerServiceProxy" 
                class="org.springframework.aop.framework.ProxyFactoryBean">

		<property name="target" ref="customerService" />

		<property name="interceptorNames">
			<list>
				<value>hijackAroundMethodBeanAdvice</value>
			</list>
		</property>
	</bean>
</beans>

File : HijackAroundMethod.java

package com.yiibai.aop;

import java.util.Arrays;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class HijackAroundMethod implements MethodInterceptor {
	@Override
	public Object invoke(MethodInvocation methodInvocation) throws Throwable {

		System.out.println("Method name : "
				+ methodInvocation.getMethod().getName());
		System.out.println("Method arguments : "
				+ Arrays.toString(methodInvocation.getArguments()));

		System.out.println("HijackAroundMethod : Before method hijacked!");

		try {
			Object result = methodInvocation.proceed();
			System.out.println("HijackAroundMethod : Before after hijacked!");

			return result;

		} catch (IllegalArgumentException e) {

			System.out.println("HijackAroundMethod : Throw exception hijacked!");
			throw e;
		}
	}
}

執(zhí)行它

package com.yiibai.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yiibai.customer.services.CustomerService;

public class App {
	public static void main(String[] args) {
		ApplicationContext appContext = new ClassPathXmlApplicationContext(
				new String[] { "applicationContext.xml" });

		CustomerService cust = (CustomerService) appContext
				.getBean("customerServiceProxy");

		System.out.println("*************************");
		cust.printName();
		System.out.println("*************************");
		cust.printURL();
		System.out.println("*************************");
		try {
			cust.printThrowException();
		} catch (Exception e) {
		}
	}
}

輸出

*************************
Method name : printName
Method arguments : []
HijackAroundMethod : Before method hijacked!
Customer name : Yiibai
HijackAroundMethod : Before after hijacked!
*************************
Method name : printURL
Method arguments : []
HijackAroundMethod : Before method hijacked!
Customer website : http://www.yiibai.com
HijackAroundMethod : Before after hijacked!
*************************
Method name : printThrowException
Method arguments : []
HijackAroundMethod : Before method hijacked!
HijackAroundMethod : Throw exception hijacked!
客戶服務(wù)類的全部方法被截獲。后來,我們展示如何使用“切入點”只攔截printName()方法。

切入點的例子

可以通過以下兩種方式相匹配的方法:
  1. 名稱匹配
  2. 正則表達(dá)式匹配

1.切入點 - 名稱匹配的例子

通過“切入點”和“advisor”攔截printName()方法。創(chuàng)建NameMatchMethodYiibaicut切入點bean,并提出要在“mappedName”屬性值來攔截方法名。
<bean id="customerYiibaicut"
        class="org.springframework.aop.support.NameMatchMethodYiibaicut">
		<property name="mappedName" value="printName" />
	</bean>
創(chuàng)建 DefaultYiibaicutAdvisor 通知 bean,通知和切入點相關(guān)聯(lián)。
<bean id="customerAdvisor"
		class="org.springframework.aop.support.DefaultYiibaicutAdvisor">
		<property name="pointcut" ref="customerYiibaicut" />
		<property name="advice" ref="hijackAroundMethodBeanAdvice" />
	</bean>
更換代理“interceptorNames”到“customerAdvisor”(它是“hijackAroundMethodBeanAdvice”)。
<bean id="customerServiceProxy"
		class="org.springframework.aop.framework.ProxyFactoryBean">

		<property name="target" ref="customerService" />
		
		<property name="interceptorNames">
			<list>
				<value>customerAdvisor</value>
			</list>
		</property>
	</bean>
全部bean配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="customerService" class="com.yiibai.customer.services.CustomerService">
		<property name="name" value="Yiibai" />
		<property name="url" value="http://www.yiibai.com" />
	</bean>

	<bean id="hijackAroundMethodBeanAdvice" class="com.yiibai.aop.HijackAroundMethod" />

	<bean id="customerServiceProxy" 
                class="org.springframework.aop.framework.ProxyFactoryBean">

		<property name="target" ref="customerService" />

		<property name="interceptorNames">
			<list>
				<value>customerAdvisor</value>
			</list>
		</property>
	</bean>

	<bean id="customerYiibaicut" 
                class="org.springframework.aop.support.NameMatchMethodYiibaicut">
		<property name="mappedName" value="printName" />
	</bean>

	<bean id="customerAdvisor" 
                 class="org.springframework.aop.support.DefaultYiibaicutAdvisor">
		<property name="pointcut" ref="customerYiibaicut" />
		<property name="advice" ref="hijackAroundMethodBeanAdvice" />
	</bean>

</beans>
再次運行,輸出
*************************
Method name : printName
Method arguments : []
HijackAroundMethod : Before method hijacked!
Customer name : Yiibai
HijackAroundMethod : Before after hijacked!
*************************
Customer website : http://www.yiibai.com
*************************
現(xiàn)在,只攔截 printName()方法。
YiibaicutAdvisor
Spring提供了YiibaicutAdvisor類來保存工作聲明advisor和切入點到不同的bean,可以使用 NameMatchMethodYiibaicutAdvisor 兩者結(jié)合成一個 bean。
<bean id="customerAdvisor"
		class="org.springframework.aop.support.NameMatchMethodYiibaicutAdvisor">
	
		<property name="mappedName" value="printName" />
		<property name="advice" ref="hijackAroundMethodBeanAdvice" />
	
	</bean>

2.切入點 - 正則表達(dá)式的例子

也可以通過使用正則表達(dá)式匹配切入點方法的名稱  – RegexpMethodYiibaicutAdvisor.

<bean id="customerAdvisor"
		class="org.springframework.aop.support.RegexpMethodYiibaicutAdvisor">
		<property name="patterns">
			<list>
				<value>.*URL.*</value>
			</list>
		</property>

		<property name="advice" ref="hijackAroundMethodBeanAdvice" />
	</bean> 

現(xiàn)在,它攔截方法名稱中有“URL”的方法。在實踐中,可以用它來管理DAO層,聲明“.*DAO.*” 攔截所有的DAO類來支持事務(wù)。