Spring AOP面向切面编程

一、介绍

AOP 即 Aspect Oriented Program 面向切面编程
首先,在面向切面编程的思想里面,把功能分为核心业务功能,和辅助功能。
所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务
所谓的辅助功能,比如性能统计,日志,事务管理等等
辅助功能在Spring的面向切面编程AOP思想里,即被定义为切面
在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发
然后把切面功能和核心业务功能 "编织" 在一起,这就叫AOP

 

举个例子方便了解aop来做什么:百度搜索aop,其中搜索功能就是核心业务,下面的“百度为您找到相关结果约22,700,000个”就是aop做的事情。

 

 

二、项目实例:

1、新建一个java项目

 包的结构如下

 

2、导入jar包,按照结构建立相关文件

3、ProductService类,输出一句话用来演示业务功能

public class ProductService {
    
    public void doSomeService(){
         
        System.out.println("doSomeService");
         }
 }

4、准备日志切面LoggerAspect,该日志切面的功能是 在调用核心功能之前和之后分别打印日志

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;
    }
}

5、application.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: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">
</beans>

在其中添加核心功能类,和切面类的声明, class="包名.类名"

    <bean name="p" class="com.yyt.service.ProductService">
    </bean>
    
    <bean id="loggerAspect" class="com.yyt.aspect.LoggerAspect"/>

指定业务功能(打印一句话)

<aop:pointcut id="loggerCutpoint" 
        expression=
        "execution(* com.yyt.service.ProductService.*(..)) "/>

指定辅助功能(在打印话之前之后输出日志)

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

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

总文件如下

<?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="p" class="com.yyt.service.ProductService">
    </bean>
    
    <bean id="loggerAspect" class="com.yyt.aspect.LoggerAspect"/>
     
    <aop:config>
        <aop:pointcut id="loggerCutpoint"
            expression=
            "execution(* com.yyt.service.ProductService.*(..)) "/>
             
        <aop:aspect id="logAspect" ref="loggerAspect">
            <aop:around pointcut-ref="loggerCutpoint" method="log"/>
        </aop:aspect>
    </aop:config>   
 
  
</beans>

6、测试类,载入xml配置文件,调用业务

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

7、结果

 

posted @ 2019-05-14 16:00  奥兔man  阅读(418)  评论(0编辑  收藏  举报