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

 不会注解的小伙伴看这里哦: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();
    }
}

结果展示:

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值