1. 程式人生 > >spring aop的使用(註解方式以及基於xml配置方式)

spring aop的使用(註解方式以及基於xml配置方式)

註解方式

*******************

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="myInterceptor" class="blog.service.MyInterceptor"/>
          <bean id="personService" class="blog.service.impl.PersonServiceBean"></bean>
          
</beans>


***************

MyInterceptor.java

***************

package blog.service;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

/**
 * 切面
 *
 */
@Aspect
public class MyInterceptor {
	@Pointcut("execution (* blog.service.impl.PersonServiceBean.*(..))")
	public void anyMethod() {
		//表示式解釋:* blog.service.impl.PersonServiceBean.*(..):第一個*號表示返回型別;
		//blog.service.impl.PersonServiceBean.*:表示PersonServiceBean類下的所有方法
		//blog.service.impl..*.*:表示blog.service.impl包及其子包下的所有類的所有方法
		//(..):表示所有的引數型別幾個數都不限
	}// 宣告一個切入點

	//會攔截引數簽名位 (String , int)或(String , Integer )型別的方法,
	//引數順序與args(uname,id)一致,引數型別由(int id,String uname)決定
	@Before("anyMethod() && args(uname,id)")
	public void doAccessCheck(int id,String uname) {
		System.out.println("前置通知:" + uname + id);
	}

	@AfterReturning(pointcut="anyMethod()",returning="result")
	public void doAfterReturning(String result) {
		System.out.println("後置通知:" + result);
	}

	@After("anyMethod()")
	public void doAfter() {
		System.out.println("最終通知");
	}

	@AfterThrowing(pointcut="anyMethod()",throwing="e")
	public void doAfterThrowing(Exception e) {
		System.out.println("例外通知:" + e.getMessage());
	}
	
	@Around("anyMethod()")
	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
		//if(){//判斷使用者是否有許可權
		System.out.println("進入方法");
		Object result = pjp.proceed();
		System.out.println("退出方法");
		//}
		return result;
	}
}


***************

PersonService.java

***************

package blog.service;


public interface PersonService {
	public String save(String name);
	public String update(String name,Integer userId);
	public String delete(int id,String dept);
	
}


***************

PersonServiceBean.java

***************

package blog.service.impl;

import blog.service.PersonService;

public class PersonServiceBean implements PersonService {
	private String username = null;

	public String getUsername() {
		return username;
	}

	public PersonServiceBean() {
	}

	public PersonServiceBean(String username) {
		this.username = username;
	}

	@Override
	public String save(String name) {
		if (name.equals("") || name == null) {
			throw new RuntimeException("出錯啦");
		}
		System.out.println("in save method!" + name);

		return "in save method!" + name;
	}

	@Override
	public String update(String name, Integer userId) {
		System.out.println("in update method! name = " + name + " id = "
				+ userId + " username = " + username);
		return "in update method!name = " + name + " id = "	+ userId;
	}

	@Override
	public String delete(int id, String dept) {
		System.out.println("id = " + id + " dept = " +  dept);
		return "in delete method! + nameid = " + id + " dept = " +  dept;
	}

}


***************

SpringAOPTest.java

***************

package junitTest;


import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import blog.service.PersonService;

public class SpringAOPTest {

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}
	
	@Test
	public void AopTest(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		PersonService service = (PersonService)ctx.getBean("personService");
		try {
			
			service.delete(12, "it");
			service.update("發多個地方",324);
			service.save("");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}


***************

執行結果

***************

基於xml配置方式

*************

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="myInterceptor" class="blog.service.MyInterceptor"/>
          <bean id="personService" class="blog.service.impl.PersonServiceBean"></bean>
          
          <aop:config>
          	<aop:aspect id="aspectBean" ref="myInterceptor" >
          		<aop:pointcut  id="point"  expression="execution (!void blog.service.impl.PersonServiceBean.*(..))"/>
          		<aop:before method="doAccessCheck" pointcut-ref="point" />
          		<aop:after-returning method="doAfterReturning" pointcut-ref="point" />
          		<aop:after-throwing method="doAfterThrowing" pointcut-ref="point"/>
          		<aop:after method="doAfter" pointcut-ref="point"/>
          		<aop:around method="doBasicProfiling" pointcut-ref="point" />
          		
          	</aop:aspect>
          </aop:config>
          
</beans>

*******************

MyInterceptor.java

*******************

package blog.service;

import org.aspectj.lang.ProceedingJoinPoint;


public class MyInterceptor {
	public void anyMethod() {
	}

	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 doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
		//if(){//判斷使用者是否有許可權
		System.out.println("進入方法");
		Object result = pjp.proceed();
		System.out.println("退出方法");
		//}
		return result;
	}
}


aop表示式的使用方法

<aop:pointcut  id="point"  expression="execution (!void blog.service.impl.PersonServiceBean.*(..))"/>

!void blog.service.impl.PersonServiceBean.*(..)中!void 表示攔截所有返回型別為非void型別的方法

java.lang.String表示攔截所有返回型別為String型別的方法

* blog.service.impl.PersonServiceBean.*(java.lang.String,..)表示攔截所有第一個引數為String型別的方法