SpringAOP之通过注解方式实现
通过注解的方式
1)导入jar包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version> 4.1 . 3 .Release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version> 4.1 . 3 .Release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version> 4.1 . 3 .Release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version> 4.1 . 3 .Release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version> 4.1 . 3 .RELEASE</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version> 1.1 . 3 </version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version> 1.2 . 16 </version> </dependency> <!-- 配置servlet--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version> 3.1 . 0 </version> <scope>provided</scope> </dependency> <!--配置jsp的依赖 --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version> 2.2 </version> <scope>provided</scope> </dependency> <!-- 配置jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version> 1.2 </version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version> 1.1 . 2 </version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version> 5.1 . 37 </version> </dependency> <dependency> <groupId>commons-dbutils</groupId> <artifactId>commons-dbutils</artifactId> <version> 1.6 </version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version> 0.9 . 5.2 </version> </dependency> <!-- 配置的 spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version> 4.1 . 3 .RELEASE</version> </dependency> <!-- 事务相关的架包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version> 4.1 . 3 .RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version> 4.1 . 3 .RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version> 4.1 . 3 .RELEASE</version> </dependency> <!-- aop --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version> 1.8 . 10 </version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version> 4.1 . 3 .RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version> 4.1 . 3 .RELEASE</version> </dependency> |
2) 创建目标类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | public interface IUserService { public void add(); public void update(); public void delete(); public void select(); } package com.test.service.impl; import com.test.service.IUserService; import org.springframework.stereotype.Service; @Service public class UserService implements IUserService { @Override public void add() { System.out.println( "这是添加用户" ); } @Override public void update() { System.out.println( "这是修改用户" ); } @Override public void delete() { System.out.println( "这是删除用户" ); } @Override public void select() { System.out.println( "这是查询用户" ); } } |
3)创建配置文件 applicationContext-aop-user.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?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:context= "http://www.springframework.org/schema/context" xmlns:aop= "http://www.springframework.org/schema/aop" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-4.0.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-4.0.xsd http: //www.springframework.org/schema/aop http: //www.springframework.org/schema/aop/spring-aop-4.0.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 扫描注解所在的位置 --> <context:component-scan base- package = "com.test" /> <!-- 使用aop注解方式--> <aop:aspectj-autoproxy ></aop:aspectj-autoproxy> </beans> |
4)创建切面类 (增强+切点)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | //切面类 (增强类+切点) @Aspect public class UserAdvice { //定义切点 @Pointcut ( "execution(* com..User*.*(..))" ) public void pc() { } //在所有包中以user开头的类中所有的方法 成功返回以后 切入 afterReturningMethod()方法 //@AfterReturning("execution(* com..User*.*(..))") @AfterReturning ( "UserAdvice.pc()" ) public void afterReturningMethod() { System.out.println( "方法执行成功" ); } //@AfterThrowing("execution(* com..User*.*(..))") @AfterThrowing ( "UserAdvice.pc()" ) public void afterThrowingMethod() { System.out.println( "出异常" ); } //环绕通知 @Around ( "execution(* com..UserDao.select(..))" ) public Object around(ProceedingJoinPoint point) throws Throwable { System.out.println( "查询所有用户信息" ); return point.proceed(); // 等价于userDao.select() } } |
修改配置文件
创建一个切面类对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?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:context= "http://www.springframework.org/schema/context" xmlns:aop= "http://www.springframework.org/schema/aop" xsi:schemaLocation="http: //www.springframework.org/schema/beans http: //www.springframework.org/schema/beans/spring-beans-4.0.xsd http: //www.springframework.org/schema/context http: //www.springframework.org/schema/context/spring-context-4.0.xsd http: //www.springframework.org/schema/aop http: //www.springframework.org/schema/aop/spring-aop-4.0.xsd http: //www.springframework.org/schema/tx http: //www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 扫描注解所在的位置 --> <context:component-scan base- package = "com.test" /> <!-- 创建切面类对象--> <bean id= "userAdvice" class = "com.test.aspects.UserAdvice" ></bean> <!-- 使用aop注解方式--> <aop:aspectj-autoproxy ></aop:aspectj-autoproxy> </beans> |
6)测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | @Test public void fun() { ClassPathXmlApplicationContext classPathXmlApplicationContext= new ClassPathXmlApplicationContext( "/applicationContext-aop-user.xml" ); IUserDao userDao= classPathXmlApplicationContext.getBean( "userDao" ,IUserDao. class ); try { userDao.add(); } catch (Exception e) { e.printStackTrace(); } userDao.select(); }pring |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)