# 5.4.6. 切面实例化模型

这是一个高级话题。如果您刚开始使用 AOP，您可以放心地跳过它，直到以后。

默认情况下，应用程序上下文中的每个切面都有一个实例。AspectJ 将此称为单例实例化模型。可以定义具有备用生命周期的切面。Spring  AspectJ支持`perthis`和`pertarget` 实例化模型；`percflow`, `percflowbelow`, 和`pertypewithin`当前不受支持。

您可以通过在`@Aspect`注解中指定一个`perthis`子句来声明一个`perthis`切面。考虑以下示例：

```java
@Aspect("perthis(com.xyz.myapp.CommonPointcuts.businessService())")
public class MyAspect {

    private int someState;

    @Before("com.xyz.myapp.CommonPointcuts.businessService()")
    public void recordServiceUsage() {
        // ...
    }
}
```

在前面的示例中，`perthis`子句的效果是为执行业务服务的每个唯一服务对象（每个唯一对象绑定到切入点表达式匹配的`this`连接点）创建一个切面实例。切面实例是在第一次在服务对象上调用方法时创建的。当服务对象超出范围时，切面超出范围。在创建切面实例之前，其中的任何通知都不会运行。一旦创建了切面实例，其中声明的通知就会在匹配的连接点处运行，但仅当服务对象是与此切面关联的对象时。有关`per`子句的更多信息，请参阅 AspectJ 编程指南。

`pertarget`实例化模型的工作方式与 完全相同`perthis`，但它在匹配的连接点为每个唯一目标对象创建一个切面实例。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.shiker.tech/spring-he-xin-gong-neng/5.-spring-mian-xiang-qie-mian-bian-cheng/5.4.-aspectj-zhi-chi/5.4.6.-qie-mian-shi-li-hua-mo-xing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
