Spring框架的面向切面(AOP)原理和配置

        以下内容整理自http://how2j.cn/k/spring/spring-aop/89.html#nowhere和http://how2j.cn/k/spring/spring-annotaion-aop/1068.html#nowhere

1.Spring的AOP特性

    AOP 即 Aspect Oriented Program 面向切面编程 

    首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能。 

    所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 

    所谓的周边功能,比如性能统计,日志,事务管理等等 

    周边功能在Spring的面向切面编程AOP思想里,即被定义为切面 

    在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发,然后把切面功能和核心业务功能 “编织” 在一起,这就叫AOP


《Spring框架的面向切面(AOP)原理和配置》

2.利用xml配置AOP

首先准备业务类ProductService

package com.how2java.service;
 
public class ProductService {
     
    public void doSomeService(){
         
        System.out.println("doSomeService");
         
    }
     
}

然后准备日志切面LoggerAspect

package com.how2java.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
 
public class LoggerAspect {
 
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        System.out.println("end log:" + joinPoint.getSignature().getName());
        return object;
    }
}

然后是配置文件applicationContext.xml

<bean name=”s” class=”com.how2java.service.ProductService”> </bean>

声明业务对象

<bean id=”loggerAspect” class=”com.how2java.aspect.LoggerAspect”/>

声明日志切面

结合
思路图

<aop:pointcut id=”loggerCutpoint” expression= “execution(* com.how2java.service.ProductService.*(..)) “/>

指定右边的核心业务功能

<aop:aspect id=”logAspect” ref=”loggerAspect”> <aop:around pointcut-ref=”loggerCutpoint” method=”log”/> </aop:aspect>

指定左边的辅助功能

然后通过aop:config把业务对象与辅助功能编织在一起。

execution(* com.how2java.service.ProductService.*(..))

这表示对满足如下条件的方法调用,进行切面操作:

*
 返回任意类型

com.how2java.service.ProductService.* 
包名以 com.how2java.service.ProductService 开头的类的任意方法

(..) 
参数是任意数量和类型

    

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
    <bean name="c" class="com.how2java.pojo.Category">
        <property name="name" value="yyy" />
    </bean>
 
    <bean name="p" class="com.how2java.pojo.Product">
        <property name="name" value="product1" />
        <property name="category" ref="c" />
    </bean>
     
    <bean name="s" class="com.how2java.service.ProductService">
    </bean>   
     
    <bean id="loggerAspect" class="com.how2java.aspect.LoggerAspect"/>
     
    <aop:config>
        <aop:pointcut id="loggerCutpoint"
            expression=
            "execution(* com.how2java.service.ProductService.*(..)) "/>
             
        <aop:aspect id="logAspect" ref="loggerAspect">
            <aop:around pointcut-ref="loggerCutpoint" method="log"/>
        </aop:aspect>
    </aop:config>    
  
</beans>

运行测试代码

package com.how2java.test;
  
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.how2java.service.ProductService;
  
public class TestSpring {
  
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
        ProductService s = (ProductService) context.getBean("s");
        s.doSomeService();
    }
}

结果如下:

《Spring框架的面向切面(AOP)原理和配置》

3.AOP基于注解的方式

首先注解配置业务类

使用@Component(“s”) 注解ProductService 类,表示其是一个bean

package com.how2java.service;
 
import org.springframework.stereotype.Component;
 
@Component("s")
public class ProductService {
    public void doSomeService(){
        System.out.println("doSomeService");
    }
     
}

然后注解配置切面

@Aspect 注解表示这是一个切面
@Component 表示这是一个bean,由Spring进行管理

@Around(value = “execution(* com.how2java.service.ProductService.*(..))”) 表示对com.how2java.service.ProductService 这个类中的所有方法进行切面操作

package com.how2java.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
 
@Aspect
@Component
public class LoggerAspect {
     
    @Around(value = "execution(* com.how2java.service.ProductService.*(..))")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("start log:" + joinPoint.getSignature().getName());
        Object object = joinPoint.proceed();
        System.out.println("end log:" + joinPoint.getSignature().getName());
        return object;
    }
}

然后是配置文件applicationContext.xml

去掉原有信息,添加如下3行

<context:component-scan base-package=”com.how2java.aspect”/> <context:component-scan base-package=”com.how2java.service”/>

扫描包com.how2java.aspect和com.how2java.service,定位业务类和切面类

<aop:aspectj-autoproxy/>

找到被注解了的切面类,进行切面配置

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/aop
   http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
   http://www.springframework.org/schema/tx
   http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
   http://www.springframework.org/schema/context     
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
    <context:component-scan base-package="com.how2java.aspect"/>
    <context:component-scan base-package="com.how2java.service"/>
    <aop:aspectj-autoproxy/> 
   
</beans>

运行测试代码以后

package com.how2java.test;
  
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.how2java.service.ProductService;
 
public class TestSpring {
  
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[] { "applicationContext.xml" });
        ProductService s = (ProductService) context.getBean("s");
        s.doSomeService();
    }
}

结果和利用xml配置的一样

《Spring框架的面向切面(AOP)原理和配置》

    原文作者:AOP
    原文地址: https://blog.csdn.net/tywiiu/article/details/79502490
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞