欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

一个小小的AopDemo

程序员文章站 2024-02-18 15:49:40
...

一、首先是Maven配置

导入spring依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>5.0.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.0.7.RELEASE</version>
</dependency>
<dependency>
    <groupId>aopalliance</groupId>
    <artifactId>aopalliance</artifactId>
    <version>1.0</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.9.1</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aspects</artifactId>
    <version>5.0.7.RELEASE</version>
</dependency>

二、配置扫描包,开启AspectJ的配置类

package com.cks.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@ComponentScan("com.cks")
@Configuration
//开启aspectJ
@EnableAspectJAutoProxy
public class AppConfig {
}

三、所需要被增强的Service

package com.cks.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {

    public void queryUser(){
        System.out.println("查询用户service");
    }

    public void addUser(){
        System.out.println("新增用户service");
    }
}

四、切面,切点设置

package com.cks.aop;

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;

@Aspect
@Component
public class SimpleAspect {

//    例: execution(* com.demo..service.*Service.add*(String))
//    com.demo 包及其子包下的service包下,类名以Service结尾,方法以add开头,参数类型为String的方法的切点
    @Pointcut("execution(* com.cks.service.*Service.add*(..))")
    public void pointOne(){}

    @Before("pointOne()")
    public void adviceOne(){
        System.out.println("添加成功!!!");
    }


    //定义execution对同一类下方法级别的切点
    @Pointcut("execution(* com.cks.service.*Service.query*(..))")
    public void pointSec(){}
    
    //对切点不同的增强
    @After("pointSec()")
    public void adviceSec(){
        System.out.println("查询成功!!!");
    }

}

五、启动测试

package com.cks;

import com.cks.config.AppConfig;
import com.cks.service.UserService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Demo {

    public static void main(String[] args) {

        AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(AppConfig.class);

        UserService userService = app.getBean(UserService.class);

        //添加用户
        userService.addUser();

        //查询用户
        userService.queryUser();
    }

}

六、查看结果

一个小小的AopDemo