Spring学习(三)——面向切面编程

一、AOP概述

AOP,面向切面编程,在进行面向切面编程之前,需要我们先搞清楚下面几个概念:

  • 通知(Advice):切面的工作叫通知(定义了切面是什么以及何时使用)。
  • 切点(PointCut):定义了切面在何处使用。
  • 切面(Aspect):通知(Advice) + 切面(PointCut)

二、引入AOP依赖

    <!--Spring AOP-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>${spring.version}</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>${spring.version}</version>
    </dependency>

三、AOP编程步骤

1. 编写切面类

package cool.gjh.aspect;

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

/**
 * 切面
 * <p>
 *     驾驶提醒类
 * </p>
 *
 * @author ACE_GJH
 */
@Aspect
@Component
public class DrivingNotes {

    @Pointcut("execution(public void cool.gjh.beans..*.drive())")
    public void driveSafety(){};

    @Before("driveSafety()")
    public void beforeDrive(){
        System.out.println("出发前:注意安全");
    }

    @After("driveSafety()")
    public void afterDrive(){
        System.out.println("停车后:停放好车辆");
    }
}

注意事项:

  1. 切面 = 通知 + 切点
  2. 切面类需要成为Bean

2. 配置类开启AspectJ自动代理

在配置类上添加注解@EnableAspectJAutoProxy

package cool.gjh.config;

import cool.gjh.zoo.Animal;
import cool.gjh.zoo.ZooFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * Spring配置类
 *
 * @author ACE_GJH
 */
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "cool.gjh")
@Configuration
public class Config {
    @Bean(name = "singleDog")
    public Animal dog(){
        return ZooFactory.getAnimalByName("Dog");
    }
    @Bean
    public Animal cat(){
        return ZooFactory.getAnimalByName("Cat");
    }
}

3. 编写测试类进行测试

package test.cool.gjh.spring;

import cool.gjh.beans.Car;
import cool.gjh.config.Config;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Config.class)
public class SpringDITest {

    @Autowired
    private Car car;

    /**
     * 测试依赖注入
     */
    @Test
    public void testInject(){
        car.drive();
    }

}

测试结果:
测试结果

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭建華

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值