AOP(面向切面编程)基于注解方式配置

发布时间:2024年01月05日

?不会注解的小伙伴看这里哦:Spring常用注解!!!-CSDN博客

pom.xml

 <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.1.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.19</version>
        </dependency>
    </dependencies>

UserDaoImpl:

package com.by.dao;

import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {

    @Override
    public void addUser(){
        System.out.println("insert into tb_user......");
    }
}

UserServiceImpl:

package com.by.service;

import com.by.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public void addUser(){
        userDao.addUser();
        //System.out.println(8/0);
    }
}

MyLogActive:(增强类)

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.advice;

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

/**
 * <p>Project: Spring - MyLogAdvice</p>
 * <p>Powered by scl On 2024-01-05 15:04:11</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
@Component
@Aspect
public class MyLogAdvice {
    @Before("execution(* com.by.service.*.*(..))")
    public void before() {
        System.out.println("前置通知、。、");
    }

    @After("execution(* com.by.service.*.*(..))")
    public void after() {
        System.out.println("最终通知、、、");
    }
    @AfterReturning("execution(* com.by.service.*.*(..))")
    public void afterReturn(){
        System.out.println("后置通知");
    }
    @AfterThrowing("execution(* com.by.service.*.*(..))")
    public void afterThrowing(){
        System.out.println("异常通知");
    }
    @Around("execution(* com.by.service.*.*(..))")
    public void around(ProceedingJoinPoint joinPoint) {
        try {
            System.out.println("前环绕通知。。。");
            joinPoint.proceed();
            System.out.println("后环绕通知。。。");
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
}

applicationContext.xml:

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        		http://www.springframework.org/schema/beans/spring-beans.xsd
        		http://www.springframework.org/schema/aop
        		http://www.springframework.org/schema/aop/spring-aop.xsd
        		http://www.springframework.org/schema/context
        		http://www.springframework.org/schema/context/spring-context.xsd">
    <!--扫描com.by下的文件,将其添加到工厂-->
    <context:component-scan base-package="com.by"></context:component-scan>

    <!--aop,自动扫描注解-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>

</beans>

测试:

/*
 * Copyright (c) 2020, 2024,  All rights reserved.
 *
 */
package com.by.web;

import com.by.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * <p>Project: Spring - Client</p>
 * <p>Powered by scl On 2024-01-05 15:01:34</p>
 * <p>描述:<p>
 *
 * @author 孙臣龙 [1846080280@qq.com]
 * @version 1.0
 * @since 17
 */
public class Client {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = applicationContext.getBean("userServiceImpl", UserService.class);
        System.out.println(userService);
        userService.addUser();
    }
}

结果展示:

文章来源:https://blog.csdn.net/qq_64847107/article/details/135413832
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。