spring增强方式常用的两种,@Around示例

引入spring的几个jar包到工程
在src下建立包AspectTest;
然后在包下创建以下类。

service接口跟实现

package AspectTest;  

import entiry.Person;

/** 
 * 我的技术网站 2016/11/15 0015. 
 */  
public interface IAService {  
    String m1(Person p3);  
    void m2(); 
     String m3(Person p1,Person p)  ;
}  
package AspectTest;

import org.springframework.stereotype.Component;  


import entiry.Person;

import javax.annotation.Resource;  

/** 
 * 我的技术网站 2016/11/15 0015. 
 */  
@Component("aservice")  
public class AServiceImpl implements IAService {  
//自定义注解Servicelock在m1上
  @Servicelock
    public String m1(Person p3)  
    {  
        System.out.println(AServiceImpl.class+".m1()");  
        return AServiceImpl.class+".m1()+=================================";  
    }  

    @Override  
    public void m2() {  
        int i=0;  
        System.out.println(10/i);  
    }  

    public String m3(Person p1,Person p)  
    {  
        System.out.println(AServiceImpl.class+".m1()");  
        return AServiceImpl.class+".m1()+=================================";  
    }


}  

方式1,直接写增强切面类

package AspectTest;  

import org.aspectj.lang.ProceedingJoinPoint;  
import org.aspectj.lang.annotation.Around;  
import org.aspectj.lang.annotation.Aspect;  
import org.springframework.core.annotation.Order;  
import org.springframework.stereotype.Component;  

/** 
 * 我的技术网站 2016/11/17 0017. 
 */  
@Component  
@Aspect  
//@Order(1000001)  
public class Around2 { 
    @Around(value = "execution(* AspectTest.*Impl.*(..))") 
    //AServiceImpl 环绕增强AServiceImpl 所有方法
    public Object exec(ProceedingJoinPoint invocation) throws Throwable {  
        System.out.println("Around2");
        Order order = this.getClass().getAnnotation(Order.class);  
        if(order!=null)  System.out.println(Around2.class+" Around通知 order = "+order.value()+" start");  
        Object result = invocation.proceed();  
        if(order!=null)  System.out.println(Around2.class+" Around通知 order = "+order.value()+" end"); 
        System.out.println("Around2----end");
        return result;  
    }  
}  


第二种通过自定义注解
注解类

package AspectTest;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(value={ElementType.PARAMETER, ElementType.METHOD})
@Retention(value=RetentionPolicy.RUNTIME)
@Documented

public  @interface Servicelock { 
     String description()  default "";
}

切面类

package AspectTest;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Scope;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
//单利多利
@Scope
@Aspect
@Order(100)
public class LockAspect {
    /**
     * 思考:为什么不用synchronized
     * service 默认是单例的,并发下lock只有一个实例
     */
    private static  Lock lock = new ReentrantLock(true);//互斥锁 参数默认false,不公平锁  

    //Service层切点     用于记录错误日志
    @Pointcut("@annotation(AspectTest.Servicelock)")  
    public void lockAspect() {

    }

    @Before("lockAspect()")
    public void exec(){ 


        Object obj = null;
        System.out.println("before1+lockAspect");

    } 
    @Around("lockAspect()")
    public  Object around(ProceedingJoinPoint joinPoint) { 
        lock.lock();
      System.out.println(  "locked/...............");
        Object obj = null;
        try {
            obj = joinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        } finally{
            lock.unlock();
            System.out.println(  "unununulocked/...............");
        }
        return obj;
    } 


   @After(value = "lockAspect()")  
   public void exec1(){  
    System.out.println("after+lockAspect");
       Order order = this.getClass().getAnnotation(Order.class);  
       if(order!=null)  System.out.println(After2.class+" After通知 order = "+order.value());  
   } 
}

测试类

package AspectTest;  

import java.util.Date;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;  

import entiry.Person;

/** 
 * 我的技术网站 2016/11/17 0017. 
 */  
public class Client {  
    public static void main(String[] args) {  
        //BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/AspectTest/spring-demo4.xml");  
        IAService aservice = context.getBean("aservice", IAService.class);  
        System.out.println("\n========================\n");  
       aservice.m1(new Person(1, "WHJ", 12, "Ss", new Date()));  

        System.out.println("\n========================\n");  
        try {  
         //   aservice.m2();  
        } catch (Exception e){  
            //暂时不输出错误信息  
            System.out.println("sssss");
            e.printStackTrace();
        }  
    }  
}  

简单xml配置

  
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
       xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"  
       xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"  
       xmlns:p="http://www.springframework.org/schema/p"  
       xsi:schemaLocation="http://www.springframework.org/schema/beans  
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.1.xsd   
    http://www.springframework.org/schema/aop   
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd   
    http://www.springframework.org/schema/tx    
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd" default-autowire="byName">  
    <aop:aspectj-autoproxy />  
    <context:component-scan base-package="AspectTest"/>  

    
beans>  

你可能感兴趣的:(随笔)