对于使用 Spring AOP 的独立 Java 应用程序,是否需要 JVM 参数(例如 -javaagent:pathto/aspectjweaver.jar
)来“开启”AOP 支持?您希望或需要什么时候这样做?
明确地说,“独立”是指 Maven 构建的、可执行的 .jar;将在各种平台上调用。
最佳答案
这取决于。如果您使用 Spring AOP 仅进行粗粒度拦截(意味着您只想拦截对 bean 的外部调用,而不是 bean 内部调用),则根本不需要 JVM 参数。只需使用如下代码:
<bean id="myInterceptor" class="com.company.interceptors.MyInterceptor"></bean>
<aop:config>
<aop:pointcut id="myPointcut"
expression="execution(* com.company.services..MyService.*(..))" />
<aop:advisor pointcut-ref="myPointcut"
advice-ref="myInterceptor" />
</aop:config>
如果这还不够,并且您需要加载时编织来建议 bean 内调用,那么您需要添加一个 JVM 参数,如 Spring 3.0 文档中所述:
Generic Java applications
You may enable Spring's support for LTW in any Java application (standalone as well as application server based) through the use of the Spring-provided instrumentation agent. To do so, start the VM by by specifying the -javaagent:path/to/spring-agent.jar option. Note that this requires modification of the VM launch script which may prevent you from using this in application server environments (depending on your operation policies).
关于java - Spring AOP 加载时编织是否需要 JVM 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11019873/