基于注解的AOP开发(快速入门以及注解配置AOP详解)
基于注解的AOP开发1、快速入门①、创建目标接口和目标类(内部有切点)创建一个itheima_spring_aop的项目,创建com.itheima.anno包,然后在里面创建目标类Target和目标接口TargetInterfacepackage com.itheima.anno;public class Target implements TargetInterface {public voi
·
基于注解的AOP开发
1、快速入门
①、创建目标接口和目标类(内部有切点)
创建一个itheima_spring_aop
的项目,创建com.itheima.anno
包,然后在里面创建目标类Target
和目标接口TargetInterface
package com.itheima.anno;
public class Target implements TargetInterface {
public void save() {
System.out.println("save running...");
}
}
package com.itheima.anno;
public interface TargetInterface {
public void save();
}
②、创建切面类(内部有增强方法)
创建切面MyAspect
类,里面有增强的方法
package com.itheima.anno;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyAspect {
public void before(){
System.out.println("前置增强....");
}
public void afterReturning(){
System.out.println("后置增强....");
}
//ProceedingJoinPoint:正在执行的连接点 ==> 切点
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前增强....");
Object proceed = pjp.proceed();//切点方法
System.out.println("环绕后增强....");
return proceed;
}
public void afterThrowing(){
System.out.println("异常抛出增强....");
}
public void after(){
System.out.println("最终增强....");
}
}
③、将目标类和切面类的对象创建权交给 spring
import org.springframework.stereotype.Component;
@Component("target")
public class Target implements TargetInterface {
//代码省略
}
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
@Component("myAspect")
public class MyAspect {
//代码省略
}
④、在切面类中使用注解配置织入关系
在src\main\resource
里面创建创建applicationContext-anno.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component("myAspect")
@Aspect//标注当前MyAspect是一个切面类
public class MyAspect {
//配置前置通知
@Before("execution(* com.itheima.anno.*.*(..))")
public void before(){
System.out.println("前置增强....");
}
@AfterReturning("execution(* com.itheima.anno.*.*(..))")
public void afterReturning(){
System.out.println("后置增强....");
}
//ProceedingJoinPoint:正在执行的连接点 ==> 切点
@Around("execution(* com.itheima.anno.*.*(..))")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前增强....");
Object proceed = pjp.proceed();//切点方法
System.out.println("环绕后增强....");
return proceed;
}
@AfterThrowing("execution(* com.itheima.anno.*.*(..))")
public void afterThrowing(){
System.out.println("异常抛出增强....");
}
@After("execution(* com.itheima.anno.*.*(..))")
public void after(){
System.out.println("最终增强....");
}
}
⑤、在配置文件中开启组件扫描和 AOP 的自动代理
<?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/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.itheima.anno"/>
<!--aop自动代理-->
<aop:aspectj-autoproxy/>
</beans>
⑥、测试
在src\test\java
里面创建com.itheima.test
包,然后创建测试AnnoTest
文件
import com.itheima.anno.TargetInterface;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-anno.xml")
public class AnnoTest {
@Autowired
private TargetInterface target;
@Test
public void test1(){
target.save();
}
}
运行结果:
环绕前增强....
前置增强....
save running...
环绕后增强....
最终增强....
后置增强....
2、注解配置AOP详解
(1)注解通知的类型
- 通知的配置语法:
@通知注解("切点表达式")
(2)切点表达式的抽取
- 同xml配置aop一样,我们可以将切点表达式抽取。抽取方式是在切面内定义方法,在该方法上使用
@Pointcut
注解定义切点表达式,然后在在增强注解中进行引用。具体如下:
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Component("myAspect")
@Aspect//标注当前MyAspect是一个切面类
public class MyAspect {
//配置前置通知
@Before("pointcut()")
public void before(){
System.out.println("前置增强....");
}
@AfterReturning("pointcut()")
public void afterReturning(){
System.out.println("后置增强....");
}
//ProceedingJoinPoint:正在执行的连接点 ==> 切点
@Around("pointcut()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("环绕前增强....");
Object proceed = pjp.proceed();//切点方法
System.out.println("环绕后增强....");
return proceed;
}
@AfterThrowing("MyAspect.pointcut()")
public void afterThrowing(){
System.out.println("异常抛出增强....");
}
@After("MyAspect.pointcut()")
public void after(){
System.out.println("最终增强....");
}
//定义切点表达式
@Pointcut("execution(* com.itheima.anno.*.*(..))")
public void pointcut(){}
}
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
已为社区贡献7条内容
所有评论(0)