开发明确
- 编写核心业务类代码(目标类的目标方法)
- 编写切面类,切面类中有增强方法
- 在配置文件中,配置织入关系
1、快速入门
原先代码可查看https://blog.csdn.net/weixin_35798336/article/details/112466111。期间有用底层实现的动态代理,实际上也就是把代理对象的工厂用配置形式实现。
-
导入AOP相关坐标
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.chen</groupId> <artifactId>Spring</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>05spring-account</module> <module>06spring-aop</module> </modules> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> </dependency> <!--包含aop,beans,core坐标--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <!--spring发现aspectj比自己本身的好,所以将其引入--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.4</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>commons-dbutils</groupId> <artifactId>commons-dbutils</artifactId> <version>1.4</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> </dependencies> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
-
创建目标接口(可自行创建)和目标类
-
创建切面类(内部增强方法)
-
将目标接口和切面类的创建交给spring
-
在配置文件中配置织入关系
<?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"> <!-- 配置Service --> <!--配置目标对象--> <bean id="accountService" class="com.chen.service.impl.AccountServiceImpl"> <!-- 注入dao --> <property name="accountDao" ref="accountDao"></property> </bean> <!-- 配置事务管理器--> <!--配置切面对象--> <bean id="txManager" class="com.chen.utils.TransactionManager"> <!-- 注入ConnectionUtils --> <property name="connectionUtils" ref="connectionUtils"></property> </bean> <!--配置织入(哪些方法需要哪些增强==通知+切点)--> <aop:config> <aop:aspect id="txManagerAdvice" ref="txManager"> <aop:pointcut id="pt1" expression="execution(* com.chen.service.impl.*.*(..))"/> <aop:before method="beginTransaction" pointcut-ref="pt1"></aop:before> <aop:after method="commit" pointcut-ref="pt1"></aop:after> <aop:after-throwing method="rollback" pointcut-ref="pt1"></aop:after-throwing> <aop:after-returning method="release" pointcut-ref="pt1"></aop:after-returning> </aop:aspect> </aop:config> <!--配置Dao对象--> <bean id="accountDao" class="com.chen.dao.impl.AccountDaoImpl"> <!-- 注入QueryRunner --> <property name="runner" ref="runner"></property> <!-- 注入ConnectionUtils --> <property name="connectionUtils" ref="connectionUtils"></property> </bean> <!--配置QueryRunner--> <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"></bean> <!-- 配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!--连接数据库的必备信息--> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/nba"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> <!-- 配置Connection的工具类 ConnectionUtils --> <bean id="connectionUtils" class="com.chen.utils.ConnectionUtils"> <!-- 注入数据源--> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
2、细节详解
-
声明配置:
<aop:config></aop:config>
-
aspect配置切面
<aop:config> <aop:aspect id="txManagerAdvice" ref="txManager"></aop:aspect> </aop:config>
id:给切面提供一个唯一标识。 ref:引用配置好的通知类 bean 的 id。
-
aop:pointcut配置切入点表达式
<aop:config> <aop:before method="beginTransaction" pointcut-ref="pt1"></aop:before> <aop:after-returning method="commit" pointcut-ref="pt1"></aop:after-returning> <aop:after method="release" pointcut-ref="pt1"></aop:after> <aop:after-throwing method="rollback" pointcut-ref="pt1"></aop:after-throwing> </aop:config>
pointcut:切入点表达式,指对类的哪些方法进行增强
method:指定通知中方法的名称。 pointct:定义切入点表达式 pointcut-ref:指定切入点表达式的引用
-
aop:xxx配置通知类型
- aop:before:用于配置前置通知。指定增强的方法在切入点方法之前执行
- aop:after-throwing:用于配置异常通知
- aop:after-returning:用于配置后置通知
- aop:after:用于配置最终通知