# 5.4.1. 启用@AspectJ 支持

要在 Spring 配置中使用 @AspectJ 切面，您需要启用 Spring 支持以基于 @AspectJ 切面配置 Spring AOP，并根据这些切面是否通知它们来自动代理 bean。通过自动代理，我们的意思是，如果 Spring 确定一个 bean 由一个或多个切面通知，它会自动为该 bean 生成一个代理来拦截方法调用并确保根据需要运行通知。

可以使用 XML 或 Java 样式的配置启用 @AspectJ 支持。无论哪种情况，您还需要确保 AspectJ 的`aspectjweaver.jar`库位于应用程序的类路径中（1.8 版或更高版本）。该库在 AspectJ 发行版的`lib`目录中或从 Maven 中央存储库中可用。

**使用 Java 配置启用 @AspectJ 支持**

要使用 Java `@Configuration`启用 @AspectJ 支持，请添加`@EnableAspectJAutoProxy` 注解，如以下示例所示：

```java
@Configuration
@EnableAspectJAutoProxy
public class AppConfig {

}
```

**通过 XML 配置启用 @AspectJ 支持**

要使用基于 XML 的配置启用 @AspectJ 支持，请使用`aop:aspectj-autoproxy` 元素，如以下示例所示：

```xml
<aop:aspectj-autoproxy/>
```

这假定您使用 [基于 XML 模式的配置](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#xsd-schemas)中所述的模式支持。有关如何在`aop`命名空间中导入标签的信息，请参阅[AOP 模式](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#xsd-schemas-aop)。


---

# 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.1.-qi-yong-aspectj-zhi-chi.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.
