Spring注解实现AOP

 

------------------------------------------

 

要进行AOP编程,首先我们要在spring的配置文件中引入aop命名空间:

<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-2.5.xsd

           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

</beans>

 

 

Spring提供了两种切面声明方式,实际工作中我们可以选用其中一种:

基于XML配置方式声明切面。

基于注解方式声明切面

 

自定的切面:(注意我们的切面一定要交给Spring管理才行,也即在XML文件里配置)

package cn.gbx.example;

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 MyIntercept {

	@Pointcut("execution (* cn.gbx.serviceimpl.PersonServiceImpl.*(..))")
	private void anyMethod() {} // 定义一个切入点
	
	
	@Before("anyMethod() && args(name)")  //利用arg来限定拥有一个参数, 参数的名字是String类型,然后得到该参数
	public void doAccessCheck(String name) {
		System.out.println("Name = " + name);
		System.out.println("前置通知");
	}
	
	@AfterReturning(pointcut = "anyMethod()", returning="name") //获得返回结果
	public void doAfterReturning(String name) {
		System.out.println("返回Name = " + name);
		System.out.println("后置通知");
	}
	
	@After("anyMethod()")
	public void doAfter() {
		System.out.println("最终通知");
	}
	
	@AfterThrowing(pointcut = "anyMethod()", throwing="e")
	public void doAfterThrowing(Exception e) {
		System.out.println(e.getMessage());
		System.out.println("例外通知");
	}
	
	@Around("anyMethod()")//环绕通知  可以在环绕通知里面坐权限的限定 struts 的连接器就是  定义格式是固定的
	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("进入方法");
		Object value = pjp.proceed();
		System.out.println("退出方法");
		return value;
	}
}

	

  

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" 
       xmlns:context="http://www.springframework.org/schema/context"
       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.gbx.serviceimpl.PersonServiceImpl"></bean>
        
         <bean id="myIntercept" class="cn.gbx.example.MyIntercept"></bean>
</beans>

  

 

posted @ 2014-02-22 15:54  E_star  阅读(348)  评论(0编辑  收藏  举报