Spring笔记(四)SpingAOP

需要的Jar包(String3.2)
com.springsource.net.sf.cglib-2.2.0.jar // 作用于cglib方式的动态代理
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aop-3.2.0.RELEASE.jar

五种增强模式

1、前置增强【before】,在业务代码之前执行增强的服务代码

示例代码如下:
AopUtil.java

// 服务类定义                                      
public class AopUtil {                             
    // 增强函数                                    
    public void showLog(){                         
        System.out.println("调用服务类功能成功!");
    }                                              
}

Bean.java

// 业务类实现                                          
public class Bean {                                    
    // 具体业务功能实现                                
    public void callBean(){                            
        System.out.println("调用Bean业务类功能成功!");
    }                                                  
}

配置文件 spring.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-2.5.xsd   
                                                                               
10               http://www.springframework.org/schema/aop                         
11                 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> 
12                                                                                 
13       <bean id="beanID"  class="aop.xml.Bean" ></bean>                          
14       <bean id="utilID"  class="aop.xml.AopUtil"></bean>                        
15                                                                                 
16       <aop:config proxy-target-class="true">                                    
17           <aop:pointcut expression="execution(public *  *.*(..))" id="pcID"/>   
18                                                                                 
19           <aop:aspect ref="utilID" >                                            
20               <aop:before method="showLog"  pointcut-ref="pcID" />              
21           </aop:aspect>                                                         
22       </aop:config>                                                             
23                                                                                 
24   </beans>

TestSpring.java

// 程序入口                                                          
public class TestSpring {                                            
    public static void main(String[] args) {                         
        ApplicationContext ac = new ClassPathXmlApplicationContext(  
                new String[] { "aop/xml/spring.xml" });              
        Bean bean = (Bean) ac.getBean("beanID");                     
        bean.callBean();                                             
    }                                                                
}

2、后置增强【after】,在业务代码之后执行执行增强的服务代码,与前置增强区别仅在于XML文件中对aop的配置

<aop:config proxy-target-class="true">                                     
        <aop:pointcut expression="execution(public *  *.*(..))" id="pcID"/>
                                                                           
        <aop:aspect ref="utilID" >                                         
            <aop:after method="showLog"  pointcut-ref="pcID" />            
        </aop:aspect>                                                      
</aop:config>

3、正常返回后增强【after-returning】,业务代码正确执行后才会执行服务代码,也就是说若业务代码如果有异常,服务代码就不会执行

<aop:config proxy-target-class="true">                                     
        <aop:pointcut expression="execution(public *  *.*(..))" id="pcID"/>
                                                                           
        <aop:aspect ref="utilID" >                                         
            <aop:after-returning method="showLog"  pointcut-ref="pcID" />  
        </aop:aspect>                                                      
</aop:config>

4、异常后增强【after-throwing】,与after-returning的执行过程相反

<aop:config proxy-target-class="true">                                     
        <aop:pointcut expression="execution(public *  *.*(..))" id="pcID"/>
                                                                           
        <aop:aspect ref="utilID" >                                         
            <aop:after-throwing method="showLog"  pointcut-ref="pcID" />   
        </aop:aspect>                                                      
</aop:config>

5、方法环绕增强【around】

<aop:config proxy-target-class="true">                                            
        <aop:pointcut expression="execution(public *  *.callBean(..))" id="pcID"/>
                                                                                  
        <aop:aspect ref="utilID" >                                                
            <aop:around method="writeLog"  pointcut-ref="pcID" />                 
        </aop:aspect>                                                             
</aop:config>
// 服务类定义
public class AopUtil {
   
    public void writeLog(ProceedingJoinPoint point) throws Throwable{
        System.out.println("调用服务类功能【writeLog】成功!");
        point.proceed();
    }
}

方法环绕增强与前面四种增强的主要区别在于,业务代码调用的控制在服务类的增强函数中 point.proceed()

XML方式配置stringAop总结:
确定动态代理方式JDK还是CGILIB有proxy-target-class属性字段的值确定,false表示采用JDK自带的动态代理,true表示采用 CGLIB动态代理;确定切入点即业务类中实现的业务方法,由属性值aop:pointcut expression确定,id=”pcID”建立业务方法的ID值execution属性值格式:
execution(修饰符 返回值 包名.类名.方法名(参数) 异常),其中修饰符、包名、类名、异常可以省略也支持通配符*,返回类型、方法名、参数不能省略但支持通配符*;
业务方法与服务方法建立关系由aop:aspect配置确定,ref=”utilID”指定服务类对象,
<aop:增强方式 method=增强方法 pointcut-ref=业务方法ID值 />

注解方式实现stringAop示范例代码:

spring.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                               
10               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd         
11                                                                                        
12               http://www.springframework.org/schema/aop                                
13                 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd           
14                                                                                        
15                 http://www.springframework.org/schema/context                          
16                 http://www.springframework.org/schema/context/spring-context-3.0.xsd" >
17                                                                                        
18       <!-- 打开注解开关, 开关打开后才能解析@符号表示的注解 -->                        
19       <context:annotation-config />                                                    
20       <!-- 扫描指定包下的注解,也就是指定需解析注解的路径 -->                          
21       <context:component-scan base-package="annotation"></context:component-scan>      
22       <!-- 告诉springAop,产生业务类的动态代理对象,并切入到对应服务类方法中 -->        
23       <aop:aspectj-autoproxy/>                                                         
24   </beans>

Bean.java 

// 业务类实现                                              
//@Component表示业务Bean实例化                             
@Component(value="beanID")                                 
public class Bean {                                        
    // 具体业务功能实现,通过@Pointcut注解注册切入点       
    @Pointcut (value="execution(public *  *.callBean(..))")
    public void callBean(){                                
        System.out.println("调用Bean业务类功能成功!");    
    }                                                      
10   }

AopUtil.java

// 服务类定义                                                                 
//@Aspect表示AopUtil为切面,@Component(value="aopUtilID")对AopUtil类进行实例化
@Aspect                                                                       
@Component(value="aopUtilID")                                                 
public class AopUtil {                                                        
    // @Before确定为增强模式为前置增强,value的值表示增强的业务函数           
    @Before (value="annotation.Bean.callBean()")                              
    public void writeLog(){                                                   
        System.out.println("调用服务类功能【writeLog】成功!");               
10       }                                                                        
11  }

TestSpring.java   // 程序入口

public class TestSpring {                                          
    public static void main(String[] args) {                       
        ApplicationContext ac = new ClassPathXmlApplicationContext(
                new String[] { "annotation/spring.xml" });         
        Bean bean = (Bean) ac.getBean("beanID");                   
        bean.callBean();                                           
    }                                                              
}

注解说明:
@Component(value=自定义ID名称)作用于需实例化的类,并将实例化后的类对象加入Bean工厂;
@Aspect 指定作用于springAop中的切面类;
@Before (value="业务方法名")指定springAop中增强模式及需要增强的业务方法,业务方法名需带包名;
@Pointcut (value="execution表达式)") 指定springAop切入点业务方法;
@Resource(name="自定义ID名称")在Bean工厂中查找对应的ID名称对象,并注入到修饰的方法中,例子如下:

private Unit unit;                  
    @Resource(name="unitID")        
    public void setUnit(Unit unit) {
        this.unit = unit;                                  
}

原文地址:https://www.cnblogs.com/jianyuan/p/4225692.html