# 5.4.2. 声明一个切面

启用 @AspectJ 支持后，在应用程序上下文中定义的任何带有 @AspectJ 切面（具有`@Aspect`注解）的类的 bean 都会被 Spring 自动检测并用于配置 Spring AOP。接下来的两个示例显示了一个不太有用的切面所需的最小定义。

这两个示例中的第一个显示了应用程序上下文中的常规 bean 定义，它指向具有`@Aspect`注解的 bean 类：

```xml
<bean id="myAspect" class="org.xyz.NotVeryUsefulAspect">
    <!-- configure properties of the aspect here -->
</bean>
```

两个示例中的第二个显示了`NotVeryUsefulAspect`类定义，它使用注解`org.aspectj.lang.annotation.Aspect`；

```java
package org.xyz;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class NotVeryUsefulAspect {

}
```

切面（用`@Aspect` 注解的类）可以具有方法和字段，与任何其他类相同。它们还可以包含切入点、通知和介绍（类型间）声明。

通过组件扫描自动检测切面您可以通过类中的`@Bean`方法将切面类注册为 Spring XML 配置中的常规 `@Configuration` bean ，或者让 Spring 通过类路径扫描自动检测它们——与任何其他 Spring 管理的 bean 相同。但是，请注意， `@Aspect`注解不足以在类路径中进行自动检测。为此，您需要添加一个单独的`@Component`注解（或者，根据 Spring 组件扫描器的规则，一个符合条件的自定义构造型注解）。

通知切面与其他切面？在 Spring AOP 中，切面本身不能成为其他切面通知的目标。类上的`@Aspect`注解将其标记为切面，因此将其排除在自动代理之外。


---

# 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.2.-sheng-ming-yi-ge-qie-mian.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.
