> For the complete documentation index, see [llms.txt](https://doc.shiker.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://doc.shiker.tech/spring-he-xin-gong-neng/5.-spring-mian-xiang-qie-mian-bian-cheng/5.9.-aspectj-dai-li-de-cheng-xu-hua-chuang-jian.md).

# 5.9. @AspectJ 代理的程序化创建

除了使用`<aop:config>` 或`<aop:aspectj-autoproxy>`声明配置中的切面之外，还可以以编程方式创建通知目标对象的代理。有关 Spring AOP API 的完整详细信息，请参阅 [下一章](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-api)。在这里，我们希望专注于使用@AspectJ 切面自动创建代理的能力。

您可以使用`org.springframework.aop.aspectj.annotation.AspectJProxyFactory`该类为一个或多个@AspectJ 切面通知的目标对象创建代理。这个类的基本用法很简单，如下例所示：

```java
// create a factory that can generate a proxy for the given target object
AspectJProxyFactory factory = new AspectJProxyFactory(targetObject);

// add an aspect, the class must be an @AspectJ aspect
// you can call this as many times as you need with different aspects
factory.addAspect(SecurityManager.class);

// you can also add existing aspect instances, the type of the object supplied must be an @AspectJ aspect
factory.addAspect(usageTracker);

// now get the proxy object...
MyInterfaceType proxy = factory.getProxy();
```

有关更多信息，请参阅[javadoc](https://docs.spring.io/spring-framework/docs/5.3.22/javadoc-api/org/springframework/aop/aspectj/annotation/AspectJProxyFactory.html)。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://doc.shiker.tech/spring-he-xin-gong-neng/5.-spring-mian-xiang-qie-mian-bian-cheng/5.9.-aspectj-dai-li-de-cheng-xu-hua-chuang-jian.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
