简体   繁体   English

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

[英]Does Spring AOP Load-Time Weaving Require JVM Arguments?

For a standalone Java application using Spring AOP, is it necessary to require JVM arguments such as -javaagent:pathto/aspectjweaver.jar to "turn on" AOP support? 对于使用Spring AOP的独立Java应用程序,是否需要要求JVM参数(例如-javaagent:pathto/aspectjweaver.jar来“打开” AOP支持? When would you want or need to do so? 您什么时候想要或需要这样做?

To be clear, "standalone" means a Maven-built, excecutable .jar; 明确地说,“独立”是指Maven构建的可执行的.jar; that will be invoked across a variety of platforms. 将在各种平台上调用。

It depends. 这取决于。 If you are using Spring AOP to do only coarse-grained interception (meaning you'd like to intercept only outer calls to a bean, but not calls inside a bean), you need no JVM arguments at all. 如果您使用Spring AOP仅执行粗粒度的拦截(这意味着您只想拦截对bean的外部调用,而不是对bean内部的调用),则根本不需要JVM参数。 Just use code like: 只需使用如下代码:

<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>

If this is not enough and you need load-time weaving to advise intra-bean calls for example, then You'd need to add a JVM argument as described in the Spring 3.0 docs: 如果这还不够,并且您需要加载时编织来建议例如Bean内部调用,那么您需要按照Spring 3.0文档中的描述添加JVM参数:

Generic Java applications 通用Java应用程序

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. 您可以通过使用Spring提供的工具代理在任何Java应用程序(独立的以及基于应用程序服务器)中启用Spring对LTW的支持。 To do so, start the VM by by specifying the -javaagent:path/to/spring-agent.jar option. 为此,通过指定-javaagent:path / to / spring-agent.jar选项来启动VM。 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). 请注意,这需要修改VM启动脚本,这可能会阻止您在应用程序服务器环境中使用它(取决于您的操作策略)。

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

Consider using compile time weaving via maven: 考虑通过Maven使用编译时编织:

<properties>
    <aspectj.version>1.6.12</aspectj.version>
</properties>

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.4</version>
            <dependencies>
                <dependency>
                    <groupId>org.aspectj</groupId>
                    <artifactId>aspectjtools</artifactId>
                    <version>${aspectj.version}</version>
                </dependency>
            </dependencies>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
                <complianceLevel>1.7</complianceLevel>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

Doing so covers all the crazy Spring AOP cases, even private transactional methods. 这样做涵盖了所有疯狂的Spring AOP案例,甚至包括私有交易方法。

通常,仅在类路径中包含weaver jar就足够了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM