介绍:Spring AOP的常用实现方法有两种,分别是基于XML文件的实现和基于注解的实现,下 面首先对基于XML的Spring AOP实现做详细讲解。因为Spring AOP中的代理对象由IoC容器自动生成,所以开发者无须过多关注代理对象生成的过程需选择连接点、创建切面、定义切点并在XML文件中添加配置信息即可。
步骤分析:
(1),配置切面
在Spring的配置文件中,配置切面使用的是apaspecD元素,该元素会将个已定 义好的 Spring Bean转换成切面Bean,因此,在使R用apaspecD元素之前,要在配置文件中先定义一个普通的 Spring Ben, SpringBean定义完成后,通过<aop:aspect> 元素的ref属性即可引用该Bean。
(2),配置切入点
在Sping的配置文件中,切人点是通pinteu>元蒙来定义的。ppnmen,无紫作gn元素的子元素定义时,表示该切人点是全局切人点。它可被多个切面共享: pnonb无0作为Capspes元素的子元索时,表示该切人点只对当前切面有效。
(3),配置通知
具体步骤:
(1),新建一个maven项目,并在pom.xml文件中导入AspectJ框架相关的JAR包的依赖,在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.halfcool</groupId>
<artifactId>springTest</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!--spring基础包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.2.22.RELEASE</version>
</dependency>
<!--spring基础包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.22.RELEASE</version>
</dependency>
<!--spring基础包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!--spring基础包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.2.21.RELEASE</version>
</dependency>
<!--spring依赖日志包-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.9.6</version>
</dependency>
</dependencies>
</project>
(2),在项目的src/main/java目录下创建一个com.halfcool.dao包,在该包下创建接口UserDao,具体代码如下:
package com.halfcool.dao;
public interface UserDao {
public void addUser();
public void deleteUser();
public void update();
public void select();
}
(3),在com.halfcool.dao包目录下创建Impl软件包,在该包下创建接口UserDaoImpl实现UserDdao中的方法,具体代码如下:
package com.halfcool.dao.Impl;
import com.halfcool.dao.UserDao;
public class UserDaoImpl implements UserDao {
@Override
public void addUser() {
System.out.println("实现了添加用户");
}
@Override
public void deleteUser() {
System.out.println("实现了删除用户");
}
@Override
public void update() {
}
@Override
public void select() {
}
}
(4),在com.halfcool包下创建XmlAdvice类,用于定义通知,具体代码如下:
package com.halfcool.dao;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
public class XmlAdvice {
public void before (JoinPoint joinPoint){
System.out.print("前置通知,模拟权限管理");
System.out.println(",目标对象:" + joinPoint.getTarget() + ",被增强处理的方法:" + joinPoint.getSignature().getName());
}
//后置返回通知
public void afterReturning (JoinPoint joinPoint){
System.out.print("后置返回通知:" + "模拟删除临时文件");
System.out.println(",被增强处理的方法:" + joinPoint.getSignature().getName());
}
//环绕通知
public Object around (ProceedingJoinPoint pjp) throws Throwable {
//开始
System.out.println("环绕开始:执行目标方法,模拟开启事务");
//执行当前目标方法
Object object = pjp.proceed();
//结束
System.out.println("环绕结束:执行目标方法后,模拟关闭事务");
return object;
}
//异常通知
public void except (Throwable e){
System.out.println("异常通知:" + "程序执行异常" + e.getMessage());
}
//后置通知,即最终通知
public void after () {
System.out.println("最终通知:模拟释放资源");
}
}
(5),在src/main/resources目录下创建applicationContext.xml文件,在该文件中引入AOP命名空间,使用<bean>元素添加SpringAOP的配置信息,具体代码如下:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--定义目标对象-->
<bean id = "userDao" class = "com.halfcool.dao.Impl.UserDaoImpl"/>
<!--定义切面-->
<bean id = "MyAspect" class = "com.halfcool.dao.XmlAdvice"/>
<!--AOP配置-->
<aop:config>
<!--配置切面-->
<aop:aspect ref = "MyAspect">
<!--配置切入点,通知增强哪些方法-->
<aop:pointcut expression="execution(* com.halfcool.dao.Impl.UserDaoImpl.*(..))" id = "myPointCut"/>
<!--将通知与切入点关联-->
<!--关联前置通知-->
<aop:before method = "before" pointcut-ref = "myPointCut"/>
<!--关联后置返回通知-->
<aop:after-returning method="afterReturning" pointcut-ref="myPointCut"/>
<!--关联环绕通知-->
<aop:around method = "around" pointcut-ref = "myPointCut"/>
<!--关联异常通知,没有发生异常不会执行增强,throwing属性设置通知的第一个参数名称-->
<aop:after-throwing method="except" pointcut-ref="myPointCut" throwing = "e" />
<!--管理后置通知,不管目标方法师傅成功都要执行-->
<aop:after method = "after" pointcut-ref = "myPointCut"/>
</aop:aspect>
</aop:config>
</beans>
(6),编写测试类:在src/test/java目录下新建testXml类,具体代码如下:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.halfcool.dao.UserDao;
public class testXml {
public static void main(String[] args) {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
userDao.addUser();
System.out.println("===========");
userDao.deleteUser();
System.out.println("===========");
userDao.update();
System.out.println("===========");
userDao.select();
}
}