Spring(4)--AOP详解

AOP详解

AOP理解:

AOP–aspect oriented programing 面向切面编程
将业务进行分离,或者说将可变的部分剥离出来,降低耦合,能够在调用某个对象的方法前、中、后回调监听类的方法。

4.1、添加依赖/引入jar包

jar包:aopalliance.jar、aspectj.weaver.jar 和 spring-aspects.jar

pom添加依赖:

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.6</version>
<!--            <scope>runtime</scope>-->
        </dependency>

<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
    <groupId>aopalliance</groupId>
    <artifactId>aopalliance</artifactId>
    <version>1.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.3.3</version>
</dependency>

4.2、使用注解的方式装载bean

步骤:

4.2.1、在applicationContext.xml中配置自动注入的扫描路径

<context:component-scan base-package="pojo"/>
<context:component-scan base-package="aspect"/>

4.2.3、定义需要监听的方法,已经监听响应的动作

Landlord.java (包租婆类,只关心签合同和收租)

package pojo;

import org.springframework.stereotype.Component;

@Component("landlord")
public class Landlord {
    public void service(){
        System.out.println("签合同");
        System.out.println("收房租");
    }
}

Broker.java (房屋中介类,需要处理包租婆以外的事情)

package aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

@Component("broker")
@Aspect
public class Broker {
    //方式一:直接创建界面
    /*@Before("execution(* pojo.Landlord.service())")
    public void before(){
        System.out.println("带租客看房");
        System.out.println("谈价格");
    }

    @After("execution(* pojo.Landlord.service())")
    public void after(){
        System.out.println("交钥匙");
    }*/

    //方式二:创建切点
    /*@Pointcut("execution(* pojo.Landlord.service())")
    public void lServie(){}

    @Before("lServie()")
    public void before(){
        System.out.println("带租客看房");
        System.out.println("谈价格");
    }

    @After("lServie()")
    public void after(){
        System.out.println("交钥匙");
    }*/

    //方式三:采用环绕通知
    @Around("execution(* pojo.Landlord.service())")
    public void around(ProceedingJoinPoint joinPoint){
        System.out.println("带租客看房");
        System.out.println("谈价格");
        try {
            joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        System.out.println("交钥匙");
    }
    
}

测试类:

@Test
public void testAOP(){
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
    Landlord landlord = context.getBean(Landlord.class);
    landlord.service();
}

在这里插入图片描述

参考:

Spring(4)——面向切面编程(AOP模块)

spring @Aspect注解,jar包的引入

解决 “通配符的匹配很全面, 但无法找到元素 ‘context:component-scan’ 的声明” 问题

解决办法就是复制完整的命名空间

<?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:context="http://www.springframework.org/schema/context"
       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/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值