1. 程式人生 > >Spring4面向切面AOP

Spring4面向切面AOP

面向切面程式設計(也叫面向方面程式設計):Aspect Oriented Programming(AOP),是軟體開發中的一個熱點,也是Spring

框架中的一個重要內容。利用AOP 可以對業務邏輯的各個部分進行隔離,從而使得業務邏輯各部分之間的耦合度
降低,提高程式的可重用性,同時提高了開發的效率。
主要的功能是:日誌記錄,效能統計,安全控制,事務處理,異常處理等等。



1,前置通知;
2,後置通知;
3,環繞通知;
4,返回通知;

5,異常通知;

寫一個service介面和 serviceImpl實現類

【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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- 父類引用指向子類例項 -->
	<bean id="employeeService" 

class="com.zhiqi.service.impl.EmployeeServiceImpl">
	</bean>
	
</beans>

【介面】

package com.zhiqi.service;

public interface EmployeeService {
	public void doSomething();
}

【實現類】

package com.zhiqi.service.impl;

import com.zhiqi.service.EmployeeService;

public class EmployeeServiceImpl implements EmployeeService {

	@Override
	public void doSomething() {
		// TODO Auto-generated method stub
		System.out.println("準備工作");
		System.out.println("業務邏輯實現");
		System.out.println("收尾工作");
	}

}

【測試】

package com.zhiqi.test;

import org.springframework.context.ApplicationContext;
import 

org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhiqi.service.EmployeeService;

public class MyTest {
	
	public static void main(String[] args) {
		ApplicationContext ac=new 

ClassPathXmlApplicationContext("beans.xml");
		EmployeeService employeeService=(EmployeeService)

ac.getBean("employeeService");
		employeeService.doSomething();

	}
}

執行:


Spring提供了很好的解耦合機制

首先引入jar包:


然後從官方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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
	

</beans>

然後定義前置通知

package com.zhiqi.advice;

import org.aspectj.lang.JoinPoint;

public class EmployeeServiceAspect {

	//前置通知
	public void doBefore(JoinPoint jp){
		System.out.println("準備工作");
	}
}
配置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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
	
	<bean id="employeeServiceAspect" class="com.zhiqi.advice.EmployeeServiceAspect"></bean>
	
	<!-- 父類引用指向子類例項 -->
	<bean id="employeeService" class="com.zhiqi.service.impl.EmployeeServiceImpl">
	</bean>
	
	<aop:config>
		<aop:aspect id="employeeServiceAspect" ref="employeeServiceAspect">
			<aop:pointcut expression="execution(* com.zhiqi.service.*.*(..))" id="businessService"/>
			<aop:before method="doBefore" pointcut-ref="businessService"/>
		</aop:aspect>
	</aop:config>
</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:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
	
	<bean id="employeeServiceAspect" class="com.zhiqi.advice.EmployeeServiceAspect"></bean>
	
	<!-- 父類引用指向子類例項 -->
	<bean id="employeeService" class="com.zhiqi.service.impl.EmployeeServiceImpl">
	</bean>
	
	<aop:config>
		<aop:aspect id="employeeServiceAspect" ref="employeeServiceAspect">
			<aop:pointcut expression="execution(* com.zhiqi.service.*.*(..))" id="businessService"/>
			<!--aop:before method="doBefore" pointcut-ref="businessService"/>
			<aop:after method="doAfter" pointcut-ref="businessService"/-->
			<aop:around method="doAround" pointcut-ref="businessService"/>
			<aop:after-returning method="doAfterReturning" pointcut-ref="businessService"/>
			<aop:after-throwing method="doAfterThrowing" pointcut-ref="businessService" throwing="ex"/>
		</aop:aspect>
	</aop:config>
</beans>

package com.zhiqi.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class EmployeeServiceAspect {

//	//前置通知
//	public void doBefore(JoinPoint jp){
//		System.out.println("類名:"+jp.getTarget().getClass().getName());
//		System.out.println("準備工作:"+" "+jp.getArgs()[0]);
//	}
//	//後置通知
//	public void doAfter(JoinPoint jp){
//		System.out.println("類名:"+jp.getTarget().getClass().getName());
//		System.out.println("收尾工作:"+" "+jp.getArgs()[0]);
//	}
	//環繞通知
	public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
		System.out.println("環繞通知-前");
		Object retValue=pjp.proceed();
		System.out.println("環繞通知-後");
		return retValue;
	}
	//返回通知
	public void doAfterReturning(JoinPoint jp){
		System.out.println("返回通知");
	}
	public void doAfterThrowing(JoinPoint jp,Throwable ex){
		System.out.println("異常通知"+" "+"異常資訊-"+ex.getMessage());
	}
}