0

我正在使用完全无 XML 的 Spring 设置,并且我成功地@EnableAspectJAutoProxy与我的@Configuration类一起使用并找到了我的方面类与 和 的@Aspect组合@Component

但是我已经到了需要按需编织/增强未使用 spring 上下文创建的实例的地步,但我能找到的只是如何使用ProxyFactory. 但是我需要手动添加建议ProxyFactory.addAdvice(..),我已经用 (例如) 写过一次@Before

我不想再重写那些了。

@EnableAspectJAutoProxy有没有办法通过使用注释来获得对(我猜?)内部创建的工厂的引用?这样我就可以做类似的事情:

@Autowired
private AspectJAutoProxyInstanceFactory f; // made up class, of course
[...]
Object bean = f.weave(obj);

或者实例化一个可以找出我的应用程序中已经存在哪些建议的工厂:

// Also a made up class, of course.
ApplicationContextAwareProxyFactory f = new ApplicationContextAwareProxyFactory(applicationContext);
Object bean = f.weave(obj);

我试图环顾四周,但似乎找不到答案。可能我只是看的不够好。提前谢谢,如果你能帮助我!

4

1 回答 1

2

我希望我正确理解了这个问题。

为了实现这一点,您可以使用没有弹簧的编译时编织。它比 spring 的基于代理的方法强大得多,并且您不必更改方面,因为 spring 已经从 AspectJ 借用了 @Aspect 注释。

您可以使用 maven 和aspectj-maven-plugin轻松实现

下面是一个配置示例:

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.4</version>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>       <!-- use this goal to weave all your main classes -->
              <goal>test-compile</goal>  <!-- use this goal to weave all your test classes -->
            </goals>
          </execution>
        </executions>
      </plugin>

如果您想编织第三方 jar 中的代码,请像这样配置它:

<plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>aspectj-maven-plugin</artifactId>
        <version>1.4</version>
        <configuration>
          <weaveDependencies>
            <weaveDependency>
              <groupId>org.agroup</groupId>
              <artifactId>to-weave</artifactId>
            </weaveDependency>
            <weaveDependency>
              <groupId>org.anothergroup</groupId>
              <artifactId>gen</artifactId>
            </weaveDependency>
          </weaveDependencies>
        </configuration>
        <executions>
          <execution>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
于 2013-03-21T23:17:39.970 回答