# 5.9程序创建@AspectJ代理

除了使用\<aop:config>或\<aop:aspectj-autoproxy>在配置中声明方面之外，还可以通过编程方式创建通知目标对象的代理。有关Spring的AOP API的完整详细信息，请参阅下一章。在这里，我们将重点介绍使用@Aspectj特性自动创建代理的能力。

可以使用org.springframework.aop.aspectj.annotation.AspectJProxyFactory类为一个或多个@Aspectj方面建议的目标对象创建代理。这个类的基本用法非常简单，如下示例所示：

```
// 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();
```


---

# 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://docs.flydean.com/spring-framework-documentation5/core-technologies/5spring-aop/5.9program-creat-aspectj-proxies.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.
