spring aop 小结

说到spring框架,大家都不会陌生,但是说到aop,可能有很多人只是听说过这个名词,但是对什么是aop以及怎么使用aop存在很多疑惑,正好最近在学spring的aop思想,现做如下总结:

一、什么是aop

大家可能都听说过oop,即面向对象的编程事项,与传统的面向过程的编程方法不同的是,oop思想讲究将服务(也就是函数方法)和数据(成员变量)封装成对象,由对象来调用方法,从而实现功能。因此,我们需要编写很多的类来实现我们需要的功能。但是,这种方式会导致类过多,而一些通用的方法虽然可以通过重构手法可以提取出来,但是还是有一些不足,那就是不能够自动化。我们希望能够在自动的增强某个函数的功能,不需要在每次调用的地方写任何代码,只需要写一次就OK。在这样的需求下,aop思想诞生了。aop---自动增强函数方法的思想,面向切面的编程方法。

下面一张图可以让你更加了解什么是aop思想:


也就是说如果有多个Service,aop就是研究怎么将安全,事务,其他这几个模块加入到service中去。下面我就用在java和spring环境下做实验。

二、java、spring环境下的aop

spring中自己对aop有一套实现,底层用的是jdk的动态代理和cglib代理,但是spring发现Aspectj对aop思想的实现更加方便我们开发,所以,spring运用拿来主义,将Aspectj中的aop实现整合到spring框架中了,所以本篇只讨论Aspectj中的aop实现,工作中一般也只用到Aspectj的aop实现。

首先我们搭建一下spring的环境:maven pom.xml如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>AspectjDemo</groupId>
  <artifactId>cn.ljtnono</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>AspectDemo</name>
  
    <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>5.0.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-expression</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.2</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.12</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.2.4.RELEASE</version>
    </dependency>
    
    <!-- cglib代理模式  类模式代理 -->
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>3.2.8</version>
    </dependency>
    </dependencies>
  
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

项目结构图如下:


这里先创建一个切面t类,这是一个普通的pojo类。(不知道什么是切面?点击这里

package cn.ljtnono.aop;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

//@Aspect
@Component("Aspect1")
public class Aspect1 {
    
    //@Before("execution( * *..SomeService*.firstService(..))")
    public void myBefore() {
        System.out.println("这是一个前置通知");
    }
}

创建一个方法,一个前置通知。编写要增强的类的接口以及实现类

package cn.ljtnono.service;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

 
public interface SomeService {
    
    
    void firstService();
    
    void secondService();
    
    void thiredService();
}

package cn.ljtnono.service;

import org.springframework.stereotype.Service;

@Service(value="SomeService")
public class SomeServiceImpl  {

    
    public void firstService() {
        // TODO Auto-generated method stub
        System.out.println("======执行了firstService方法");
    }

    
    public void secondService() {
        // TODO Auto-generated method stub
        System.out.println("======执行了secondService方法");

    }

    
    public void thiredService() {
        // TODO Auto-generated method stub

        System.out.println("======执行了thiredService方法");
    }
    
    

}

在spring配置文件中配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx.xsd
                        http://mybatis.org/schema/mybatis-spring
                        http://mybatis.org/schema/mybatis-spring.xsd
                        http://www.springframework.org/schema/cache
                        http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
    
    <!-- 开启注解配置 -->
    <context:component-scan base-package="cn.ljtnono" />
    
    <aop:config>
        <aop:pointcut expression="execution( * *..SomeService*.firstService(..))" id="myPointCut"/>
        <aop:aspect ref="Aspect1">
            <aop:before method="myBefore" pointcut-ref="myPointCut"/>
        </aop:aspect>
    </aop:config>
    
</beans>

aop:config中的配置:首先要配置一个切入点,然后配置切面的实现。这里应该容易理解。这其中需要aspect类和目标对象被容器识别,也就是必须交给spring管理这两个对象。其中execution表达式详解见我的另一篇博文。

现在测试:

package cn.ljtnono.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.ljtnono.service.SomeService;
import cn.ljtnono.service.SomeServiceImpl;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class Aspect1Test {
    
    
    
    
    @Test
    public void testBefore() {
        
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        SomeServiceImpl service = (SomeServiceImpl) ac.getBean("SomeService");
        service.firstService();
    }
}

至此,一个简单的aop demo完成了,有不懂的可以查看以下资料:

AOP execution表达式详解:https://www.jianshu.com/u/0eac251981be

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,566评论 18 139
  • 本文是我自己在秋招复习时的读书笔记,整理的知识点,也是为了防止忘记,尊重劳动成果,转载注明出处哦!如果你也喜欢,那...
    波波波先森阅读 12,267评论 6 86
  • 今天听了企管部闫拼命三郎的汇报材料 真佩服他的学习力
    小强2004阅读 231评论 0 0
  • 一、什么样运营方案是有价值的? 总结起来一句话:能够落地执行,并且提高转化率的方案都是有价值的。所有的运营方案其实...
    诚四岁阅读 1,283评论 0 2
  • 过年这段时间,感觉就像做梦一样,我第一次怦然心动,第一个过年喜欢宅在家。我能每天睡到自然醒,我起床就有好吃好喝的。...
    shine曼阅读 181评论 0 3