網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
首先需要導(dǎo)入以下依賴(lài)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>6.0.6</version>
</dependency>
<!-- spring核心 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
進(jìn)行Spring配置類(lèi)的聲明
@ComponentScan 用于對(duì)文件中bean的掃描,類(lèi)似于將bean加載到容器當(dāng)中。
@Configuration用于聲明這是一個(gè)配置類(lèi)
@ComponentScan(value = "com.mingyu")用來(lái)掃描加載bean
@EnableAspectJAutoProxy用來(lái)開(kāi)啟我們的AOP注解,用于啟動(dòng)Spring AOP 的功能。
package com.mingyu.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@ComponentScan(value = "com.mingyu")
@Configuration
//用于開(kāi)啟AOP注解
@EnableAspectJAutoProxy
public class springConfig {
}
這是我們的功能增強(qiáng)類(lèi)
@Aspect用于聲明這個(gè)類(lèi)是用于功能拓展的
@Pointcut()用于定義切點(diǎn),即要對(duì)哪個(gè)函數(shù)進(jìn)行功能增強(qiáng)。
package com.mingyu.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;
@Component
//用于定義一個(gè)切面(說(shuō)明這個(gè)類(lèi)是用于功能拓展的)
@Aspect
public class aspect {
@Pointcut("execution(* com.mingyu.dao.*.*(..))" )
public void blank(){}
@Before("blank()")
public void start(){
System.out.println("before執(zhí)行");
}
@After("blank()")
public void after(){
System.out.println("after執(zhí)行");
}
}
最后進(jìn)行測(cè)試
import com.mingyu.aspect.aspect;
import com.mingyu.config.springConfig;
import com.mingyu.dao.studentDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(springConfig.class);
studentDao sd = (studentDao) context.getBean("student");
sd.study();
sd.save();
}
}
原文鏈接:https://blog.csdn.net/m0_72903413/article/details/140367661
- 上一篇:沒(méi)有了
- 下一篇:沒(méi)有了
相關(guān)推薦
- 2023-07-16 spring boot多模塊打包 運(yùn)行
- 2022-11-27 C語(yǔ)言結(jié)構(gòu)體字節(jié)對(duì)齊的實(shí)現(xiàn)深入分析_C 語(yǔ)言
- 2023-01-12 C#實(shí)現(xiàn)讀寫(xiě)ini配置文件的方法詳解_C#教程
- 2022-06-01 詳解Pandas中stack()和unstack()的使用技巧_python
- 2022-05-11 在 WebStorm 中誤添加自定義的 HTML 屬性,如何刪除
- 2022-07-21 SpringBoot默認(rèn)開(kāi)啟AOP,采用Cglib代理方式?(Spring AOP快速入門(mén))
- 2024-04-05 springboot項(xiàng)目啟動(dòng)失敗,不打印報(bào)錯(cuò)詳細(xì)信息(啟動(dòng)打印日記問(wèn)題)
- 2022-10-28 Templates實(shí)戰(zhàn)之更優(yōu)雅實(shí)現(xiàn)自定義View構(gòu)造方法詳解_Android
- 欄目分類(lèi)
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支