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

Spring AOP實(shí)例(Pointcut,Advisor)

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

再次回顧上一個(gè) 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ù)類的全部方法被截獲。后來,我們展示如何使用“切入點(diǎn)”只攔截printName()方法。

切入點(diǎn)的例子

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

1.切入點(diǎn) - 名稱匹配的例子

通過“切入點(diǎn)”和“advisor”攔截printName()方法。創(chuàng)建NameMatchMethodYiibaicut切入點(diǎn)bean,并提出要在“mappedName”屬性值來攔截方法名。
<bean id="customerYiibaicut"
        class="org.springframework.aop.support.NameMatchMethodYiibaicut">
		<property name="mappedName" value="printName" />
	</bean>
創(chuàng)建 DefaultYiibaicutAdvisor 通知 bean,通知和切入點(diǎn)相關(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>
再次運(yùn)行,輸出
*************************
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和切入點(diǎn)到不同的bean,可以使用 NameMatchMethodYiibaicutAdvisor 兩者結(jié)合成一個(gè) bean。
<bean id="customerAdvisor"
		class="org.springframework.aop.support.NameMatchMethodYiibaicutAdvisor">
	
		<property name="mappedName" value="printName" />
		<property name="advice" ref="hijackAroundMethodBeanAdvice" />
	
	</bean>

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

也可以通過使用正則表達(dá)式匹配切入點(diǎn)方法的名稱  – 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”的方法。在實(shí)踐中,可以用它來管理DAO層,聲明“.*DAO.*” 攔截所有的DAO類來支持事務(wù)。