java - Spring AOP 加载时编织是否需要 JVM 参数?

标签 java spring jvm aop aspectj

对于使用 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).

请参阅此处 http://static.springsource.org/spring/docs/3.0.0.RC2/reference/html/ch07s08.html#aop-aj-ltw-environments

关于java - Spring AOP 加载时编织是否需要 JVM 参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11019873/

相关文章:

Spring OAuth2 - 创建访问 token

java - Java中栈中函数调用次数的限制是如何定义的?

java - 如何获取Floatbuffer存储的float[]?

Java如何调用另一个类?

java - 如何在 android 的异步函数中连续接收 UDP?

java - 使用 JDBC 的 Spring session 插入失败

java - 如何分解 Java 8 和 SLF4J 中的条件日志检查?

java - 如何在 Spring Boot 中处理两个模型之间的多对多关系并围绕它们设计剩余调用?

java - 多核上的jvm

java - JVM 错误? java.lang.VerifyError : Bad type on operand stack