天天看點

<七>AOP面向切面——配置bean.xml檔案方式聲明切面(附源碼)

上一節為注解方式聲明切面,現在通過配置bean.xml檔案的形式來配置。

1、将上面的MyInterceptor變成一個普通的java類,即去掉@Aspect這樣的注解。并将參數去掉。

package cn.itcast.service;

import org.aspectj.lang.ProceedingJoinPoint;

/**
 * 切面
 *
 */

public class MyInterceptor {

	public void doAccessCheck() {
		System.out.println("前置通知:");
	}

	public void doAfterReturning()
	{
		System.out.println("後置通知");
	}

	public void doAfter(){
		System.out.println("最終通知");
	}
	
	public void doAfterThrowing(){
		System.out.println("例外通知");
	}

	public Object doBasicProfilling(ProceedingJoinPoint pjp) throws Throwable{
		System.out.println("進入方法");
		Object result=pjp.proceed();
		System.out.println("退出方法");
		return result;
	}
}
           

2、beans.xml檔案

<?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:context="http://www.springframework.org/schema/context" 
       xmlns:aop="http://www.springframework.org/schema/aop"      
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
        <aop:aspectj-autoproxy/> 
       <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/>
       <bean id="aspectbean" class="cn.itcast.service.MyInterceptor"/>
       <aop:config>
       		<aop:aspect id="asp" ref="aspectbean">
       			<aop:pointcut id="mycut"
       			expression="execution(* cn.itcast.service.impl.PersonServiceBean.*(..))" />
       			<aop:before pointcut-ref="mycut" method="doAccessCheck" />
       			<aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
			  	<aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
			  	<aop:after pointcut-ref="mycut" method="doAfter"/>
			  	<aop:around pointcut-ref="mycut" method="doBasicProfilling"/>
       		</aop:aspect>
       </aop:config>
</beans>
           

說明:

<aop:pointcut id="mycut" expression="execution(java.lang.String cn.itcast.service.impl.PersonServiceBean.*(..))" />
将會隻攔截cn.itcast.service.impl.PersonServiceBean類中的傳回值為string類型的方法

<aop:pointcut id="mycut" expression="execution(* cn.itcast.service.impl.PersonServiceBean.*(java.lang.String,..))" />
将會隻攔截cn.itcast.service.impl.PersonServiceBean類中的參數有string類型的方法

<aop:pointcut id="mycut" expression="execution(!void cn.itcast.service.impl.PersonServiceBean.*(..))" />
将會隻攔截cn.itcast.service.impl.PersonServiceBean類中的傳回值不為空的方法

spring發現前面定義的切口的類有實作接口,則用jdk的建立代理技術進行建立代理對象;
如果沒有實作接口,則用CGlib來建立代理對象
           

3、測試SpringAOPTest.java

package junit.test;


import org.junit.BeforeClass;
import org.junit.Test;


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

import cn.itcast.service.PersonService;

public class SpringAOPTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@Test public void interceptorTest(){
		ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
		PersonService personService = (PersonService)cxt.getBean("personService");
		personService.save("chen");
		personService.getPersonName(3);
		//personService.update("ning", 4);
	}
}
           

完成。

源碼下載下傳:

點選打開連結