1.配置文件pom.xml
<?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.whhp.k9501</groupId>
<artifactId>U3_Day06_Spring01</artifactId>
<version>1.0-SNAPSHOT</version>
<name>U3_Day06_Spring01</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<!--aop联盟jar-->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!--切面-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.21.RELEASE</version>
</dependency>
<!--切面-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</project>
若是jdk1.9则需要加上
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
2.一些概念
*连接点:实现类中的所有方法
*核心代码 :一个程序中必须被执行的代码
*切入点:准备被切入的方法
*通知/增强点 :就是前置通知和后置通知
- 切入面:当通知点/增强点和切入点结合之后就变成了切入面,也就是被增强的部分。
<!--
AOP
1、目标类
2、增强类
3、织入
-->
<!-- 目标类 -->
<bean id="bocDao" class="com.whpu.k16035.aop.dao.impl.BOcDaoImpl"></bean>
<!--增强类 -->
<bean id="security" class="com.whpu.k16035.aop.security.Security"></bean>
<bean id="logger" class="com.whpu.k16035.aop.log.Logger"></bean>
<bean id="cc" class="com.whpu.k16035.aop.cache.ClearCache"></bean>
<!--aop织入-->
<aop:config>
<!--配置切入点 要被增强的方法
id:切入点的名称
expression : 切入点表达式
-->
<aop:pointcut id="pointCut" expression="execution(* com.whpu.k16035.aop.dao.impl.*.*(..) )"></aop:pointcut>
<!--切面
前置切面中:
ref:引入增强类/通知类
order:前置通知,值越小越先执行
后置通知,值越大越先执行
-->
<!--配置安全模块切面-->
<aop:aspect ref="security" order="1">
<!--前面通知-->
<aop:before method="isSecurity" pointcut-ref="pointCut"></aop:before>
</aop:aspect>
<!--配置日志管理模块切面-->
<aop:aspect ref="logger" order="2">
<!--后置通知-->
<aop:after method="log" pointcut-ref="pointCut"></aop:after>
</aop:aspect>
<!--配置缓存管理模块切面-->
<aop:aspect ref="cc" order="1">
<!--后置通知-->
<aop:after method="clear" pointcut-ref="pointCut"></aop:after>
</aop:aspect>
</aop:config>
3.test文件
@Test
public void proxyAopTest(){
//动态代理
ApplicationContext app = new ClassPathXmlApplicationContext("aop/beans_aop..xml");
BOcDao bOcDao = (BOcDao)app.getBean("bocDao");
bOcDao.select();
}
作者:吃芒果干的熊孩子
来源:CSDN
原文:https://blog.csdn.net/qq_43479839/article/details/92816832