12.2.6. Annotation-based AOP development
12.2.6.1 Quick Start
Annotation-based aop development steps:
①Create target interface and target class (internal cut point)
②Create an aspect class (with internal enhancement methods)
③ Hand over the object creation rights of the target class and the aspect class to spring
④ Use annotations to configure the weaving relationship in the aspect class
⑤ Turn on component scanning and AOP automatic proxy in the configuration file
⑥ test
①Create target interface and target class (internal cut point)
public interface TargetInterface {
public void method();
}
public class Target implements TargetInterface {
@Override
public void method() {
System.out.println("Target running....");
}
}
②Create an aspect class (with internal enhancement methods)
//切面类
public class MyAspect {
//前置增强方法
public void before(){
System.out.println("前置代码增强.....");
}
}
③ Hand over the object creation rights of the target class and the aspect class to spring
//实现目标接口
@Component("target")
public class Target implements TargetInterface {
@Override
public void method() {
System.out.println("Target running....");
}
}
@Component("myAspect")
public class MyAspect {
public void before(){
System.out.println("前置代码增强.....");
}
}
④ Use annotations to configure the weaving relationship in the aspect class
//注解配置切面类,配置织入
@Component("myAspect")
@Aspect
public class MyAspect {
@Before("execution(* com.itheima.aop.*.*(..))") //前置增强
public void before(){
System.out.println("前置代码增强.....");
}
}
⑤ Turn on component scanning and AOP automatic proxy in the configuration file
<!--组件扫描-->
<context:component-scan base-package="com.itheima.aop"/>
<!--aop的自动代理-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
⑥ Test code
//测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {
@Autowired
private TargetInterface target;
@Test
public void test1(){
target.method();
}
}
12.2.6.2 Annotation Configuration AOP Details
1) Types of annotation notifications
Notification configuration syntax: @notification annotation ("pointcut expression")
2) Extraction of point-cutting expressions
Like the xml configuration
aop, we can extract pointcut expressions. The extraction method is to define a method in the aspect, use the @Pointcut annotation to define the pointcut expression on the method, and then refer to it in the enhanced annotation. details as follows:
@@Component("myAspect")
@Aspect
public class MyAspect {
@Before("MyAspect.myPoint()")
public void before(){
System.out.println("前置代码增强.....");
}
@Pointcut("execution(* com.itheima.aop.*.*(..))")
public void myPoint(){
}
}
12.2.6.3 Knowledge points
- Annotate aop development steps
① Use @Aspect to mark the aspect class
② Use the @notification annotation to mark the notification method
③ Configure aop automatic proxy aop in the configuration file:aspectj-autoproxy/
- notification annotation type
12.3. The role of Aop in Spring
Provides declarative transactions; allows user-defined aspects
- Cross-cutting concerns: Methods or functions that span multiple modules of an application. That is, the part that has nothing to do with our business logic, but we need to pay attention to, is the cross-cutting concern. Such as logging, security, caching, transactions, etc... .
- ASPECT: A special object where crosscutting concerns are modularized. That is, it is a class.
- Advice: The work that the aspect must complete. That is, it is a method in the class.
- Target: the object to be notified.
- Proxy (Poy): An object created after a notification is applied to a target object.
- Pointcut (PointCut): The definition of the "place" where the aspect advice is executed.
- Connection point (JointPoint): The execution point that matches the pointcut.
In Spring AOP, the cross-cutting logic is defined through Advice, and Spring supports 5 types of Advice: