掘金日新计划 · 8 月更文挑战第1天

129 阅读1分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第1天,点击查看活动详情

java基于注解开发Spring AOP

AOP是什么:AOP为Aspect Oriented Programming的缩写,意为:[面向切面编程]方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是[函数式编程]的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的[耦合度]降低,提高程序的可重用性,同时提高了开发的效率。

1.创建工程

Dingtalk_20220801150443.jpg

2.pom文件引入依赖

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.5</version>
    </dependency>
</dependencies>

3.resources 新增application.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<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"
       xmlns="http://www.springframework.org/schema/beans"
       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">

<!--  引入对应的扫描  -->
    <context:component-scan base-package="com.aop"></context:component-scan>
<!--  引入切面代理-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

4.创建售卖书本类

package com.aop;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;
@Service
public class BookShop {
    public void sellingBooks(){
        System.out.println("卖出一本书");
    }
}

5.创建切面类,并且定义售前和售后方法

package com.aop.aspect;

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

@Aspect
@Component
public class MethodPro {
    @Before("execution(public * com.aop..*.*(..))")
    public void preSales(){
        System.out.println("售前服务");
    }
    @After("execution(public * com.aop..*.*(..))")
    public void afterSales(){
        System.out.println("售出服务");
    }
}

6.调用例子

import com.aop.BookShop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringApplication {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        BookShop bookShop = context.getBean("bookShop",BookShop.class);
        bookShop.sellingBooks();

    }
}

7.输出结果:

Dingtalk_20220801144530.jpg
基于注解我们不必在xml文件中写入大量的bean,省去了很多麻烦, 我们只需要在需要的代码中写入对应关键字
服务层使用:@Service;
持久层使用 @Repository
对象注入使用 @Resource 其中切面方法我们使用
@Aspect
@Component 即可定义
如果方法调用之前调用,我们则在方法使用关键字@Before();方法之后调用,使用@After();