天天看點

Spring aspect 兩種方式實作五種增強

    我們都知道spring中AOP(面向切面程式設計)支援多種增強類型,使之我們能更注重核心業務,而那些交叉業務(切面)則由增強來處理,我們來看一下配置增強的兩種方式:注解和XML

注解:

1.引入jar包(上傳圖檔不能用,隻能粘貼了)

aopalliance.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

commons-logging-1.2.jar

log4j-1.2.16.jar

spring-aop-4.2.0.RELEASE.jar

spring-aspects-4.2.0.RELEASE.jar

spring-beans-4.2.0.RELEASE.jar

spring-context-4.2.0.RELEASE.jar

spring-core-4.2.0.RELEASE.jar

spring-expression-4.2.0.RELEASE.jar

spring-test-4.2.0.RELEASE.jar

2.cn.aop---MyAspect.java(我們可以注釋其他一個個測,因為抛出異常增強需要在UserdaoImpl.java中建構一個錯誤) 

package cn.aop;

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;

@Aspect
public class MyAspect {
    //前置
@Before("execution(public * *(..))")//execution(public * *(..))所有方法增強
public void beforeAspect(){
    
    System.out.println("前置增強");
}

//後置
@AfterReturning("execution(public * *(..))")
public void AfterReturningAspect(){
    
    System.out.println("後置增強");
}

//環繞
/*@Around("execution(public * *(..))")
public void AroundAspect(ProceedingJoinPoint po){
    
    System.out.println("環繞前增強");
    try {
        po.proceed();
    } catch (Throwable e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("環繞後增強");
}*/

//異常
@AfterThrowing("execution(public * *(..))")
public void AfterThrowingAspect(){
    
    System.out.println("異常增強");
}

//最終
@After("execution(public * *(..))")
public void AfterAspect(){
    
    System.out.println("最終增強");
}
}      

3.cn.entity--Userdao.java

package cn.entity;

public interface Userdao {
public void dofirst();
public void dosecond();
}      

4.cn.entity--UserdaoImpl.java

package cn.entity;

public class UserDaoImpl implements Userdao{

    @Override
    public void dofirst() {
        // TODO Auto-generated method stub
        System.out.println("例子一");
    }

    @Override
    public void dosecond() {
        // TODO Auto-generated method stub
        System.out.println("例子二");
    }

}      

5.cn.test--ASpectTest.java

package cn.test;

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

import cn.entity.Userdao;

public class ASpectTest {
@Test
public void AspectTestBefore(){
     ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
      Userdao dao = (Userdao)context.getBean("daoimpl");
      dao.dofirst();
      dao.dosecond();
      
    
 }
}      

6.applicationContext.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-4.1.xsd">
 <!-- 目标對象-->
   <bean id="daoimpl" class="cn.entity.UserDaoImpl"></bean>
   <!-- 增強 -->
   <bean class="cn.aop.MyAspect"></bean>
   <aop:aspectj-autoproxy/>
    </beans>
          

XML:()

其他不變,cn.aop---MyAspect.java這個類改為(注解去掉,變為普通類):

package cn.aop;

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;

@Aspect
public class MyAspect {
    //前置

public void beforeAspect(){
    
    System.out.println("前置增強");
}

//後置

public void AfterReturningAspect(){
    
    System.out.println("後置增強");
}


public void AroundAspect(ProceedingJoinPoint po){
    
    System.out.println("環繞前增強");
    try {
        po.proceed();
    } catch (Throwable e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    System.out.println("環繞後增強");
}

//異常

public void AfterThrowingAspect(){
    
    System.out.println("異常增強");
}

//最終

public void AfterAspect(){
    
    System.out.println("最終增強");
}
}      

applicationContext.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-4.1.xsd">
 <!-- 目标對象-->
   <bean id="daoimpl" class="cn.entity.UserDaoImpl"></bean>
   <!-- 增強 -->
   <bean id="aspectAdvice" class="cn.aop.MyAspect"></bean>
  <aop:config>
    
     <aop:pointcut expression="execution(public * *(..))" id="allpointcut"/>
    <aop:aspect ref="aspectAdvice">
    <!-- 前置 -->
      <aop:before method="beforeAspect" pointcut-ref="allpointcut"/>
      <!-- 後置 -->
      <aop:after-returning method="AfterReturningAspect" pointcut-ref="allpointcut"/>
      <!-- 環繞 -->
      <aop:around method="AroundAspect" pointcut-ref="allpointcut"/>
      <!-- 異常抛出 -->
      <aop:after-throwing method="AfterThrowingAspect" pointcut-ref="allpointcut"/>
      <!-- 最終增強 -->
      <aop:after method="AfterAspect" pointcut-ref="allpointcut"/>
      
    </aop:aspect>
  </aop:config>
    </beans>
          

   今天就到這裡!!!

轉載于:https://www.cnblogs.com/wth1129/p/5950306.html