我正在使用完全无 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);
我尝试环顾四周,但似乎找不到答案。也许我只是不够善于观察。 如果您能帮助我,请先致谢!
最佳答案
我希望我正确理解了这个问题。
要实现这一点,您可以在没有 spring 的情况下使用编译时编织。它比 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>
关于java - "Manual"使用 @EnableAspectJAutoProxy 编织 bean 发现方面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15559649/