1. AOP注解方式入门程序
创建maven工程,导入坐标。编写接口,完成IOC的操作。步骤略。
编写切面类
给切面类添加注解 @Aspect,编写增强的方法,使用通知类型注解声明
1.配置xml扫描注解
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.aopImpl"></context:component-scan>
</beans>
2.配置注解
@Component
public class User {
//连接点/切入点
public void add(){
System.out.println("add......");
}
}
给切面类添加注解 @Aspect,编写增强的方法,使用通知类型注解声明
@Component
@Aspect //生成代理对象
public class UserProxy {
}
3.配置文件中开启自动代理
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.aopImpl"></context:component-scan>
<!--开启Aspect生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
4.通知类型注解
@Before – 前置通知
@AfterReturing – 后置通知
@Around – 环绕通知(目标对象方法默认不执行的,需要手动执行)
@After – 最终通知
@AfterThrowing – 异常抛出通知
@Component
@Aspect //生成代理对象
public class UserProxy {
//增强/通知 ---》前置通知
@Before(value = "execution(* com.*.User.add(..))")
public void before(){
System.out.println("before.............");
}
// 环绕通知
@Around(value = "execution(* com.*.User.add(..))")
public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("before.............");
// 执行被增强的方法
proceedingJoinPoint.proceed();
System.out.println("after.............");
}
// 最终通知
@After(value = "execution(* com.*.User.add(..))")
public void after() {
System.out.println("after.............");
}
//异常通知
@AfterThrowing(value = "execution(* com.*.User.add(..))")
public void afterThrowing() {
System.out.println("afterThrowing.............");
}
//后置通知
@AfterReturning(value = "execution(* com.*.User.add(..))")
public void afterReturning() {
System.out.println("afterReturning.............");
}
}
5.测试类
@Test
public void aopTest1(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
user.add();
}
本文介绍了如何在Spring框架中通过AOP注解进行切面编程,包括创建Maven工程、配置XML扫描注解、编写切面类和通知类型注解,以及实战演示前置、后置、环绕和最终通知的应用。
862

被折叠的 条评论
为什么被折叠?



