此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Framework 6.2.0! |
将 AspectJ 与 Spring 应用程序一起使用
到目前为止,我们在本章中介绍的所有内容都是纯粹的 Spring AOP。在本节中, 我们看看如何使用 AspectJ 编译器或 weaver 来代替 OR IN 如果您的需求超出了 Spring AOP 提供的功能,则可添加到 Spring AOP 中 独自。
Spring 附带了一个小型的 AspectJ 方面库,该库可在
distribution 为spring-aspects.jar
.您需要按顺序将其添加到 Classpath 中
以使用其中的 aspects。使用 AspectJ 对 Spring 和 Spring 的其他 Spring 方面进行依赖注入 Domain Objects 和 AspectJ 讨论
此库的内容以及如何使用它。使用 Spring IoC 配置 AspectJ 方面 讨论了如何
dependency 注入使用 AspectJ 编译器编织的 AspectJ 切面。最后,Spring 框架中使用 AspectJ 的加载时编织 提供了对 Spring 应用程序的加载时编织的介绍
使用 AspectJ。
使用 AspectJ 与 Spring 进行依赖注入域对象
Spring 容器实例化并配置应用程序中定义的 bean
上下文。也可以要求 bean factory 配置预先存在的
object,给定包含要应用的配置的 bean 定义的名称。spring-aspects.jar
包含一个 annotation-driven 切面,该 aspect 利用此
允许对任何对象进行依赖关系注入的能力。该支持旨在
用于在任何容器的控制之外创建的对象。域对象
通常属于这一类,因为它们通常是使用new
运算符,或者通过 ORM 工具作为数据库查询的结果。
这@Configurable
annotation 将类标记为符合 Spring 驱动的条件
配置。在最简单的情况下,您可以将其单独用作标记注释,因为
以下示例显示:
-
Java
-
Kotlin
package com.xyz.domain;
import org.springframework.beans.factory.annotation.Configurable;
@Configurable
public class Account {
// ...
}
package com.xyz.domain
import org.springframework.beans.factory.annotation.Configurable
@Configurable
class Account {
// ...
}
当以这种方式用作标记接口时, Spring 会配置
带注释的类型 (Account
)通过使用 Bean 定义(通常为
prototype-scoped),其名称与完全限定类型名称相同
(com.xyz.domain.Account
).由于 Bean 的默认名称是
其类型的完全限定名称,这是声明原型定义的便捷方式
是省略id
属性,如下例所示:
<bean class="com.xyz.domain.Account" scope="prototype">
<property name="fundsTransferService" ref="fundsTransferService"/>
</bean>
如果要显式指定要使用的原型 bean 定义的名称,则 可以直接在 Comments 中执行此作,如下例所示:
-
Java
-
Kotlin
package com.xyz.domain;
import org.springframework.beans.factory.annotation.Configurable;
@Configurable("account")
public class Account {
// ...
}
package com.xyz.domain
import org.springframework.beans.factory.annotation.Configurable
@Configurable("account")
class Account {
// ...
}
Spring 现在查找名为account
并将其用作
定义以配置新的Account
实例。
您还可以使用自动装配来避免在
都。要让 Spring 应用自动装配,请使用autowire
属性的@Configurable
注解。您可以指定@Configurable(autowire=Autowire.BY_TYPE)
或@Configurable(autowire=Autowire.BY_NAME)
对于 按类型或按名称自动装配,
分别。作为替代方案,最好指定显式的、注解驱动的
依赖项注入@Configurable
beans 通过@Autowired
或@Inject
在字段或方法级别(有关更多详细信息,请参阅基于注释的容器配置)。
最后,您可以为新的
created 和 configured 对象dependencyCheck
属性(例如@Configurable(autowire=Autowire.BY_NAME,dependencyCheck=true)
).如果此属性为
设置为true
,Spring 在配置后验证所有属性(其
不是基元或集合)的
请注意,单独使用 Comments 不会执行任何作。它是AnnotationBeanConfigurerAspect
在spring-aspects.jar
作用于
注释。从本质上讲,该 aspect 表示,“从
一个带有 Comments 的类型的新对象@Configurable
中,配置新创建的对象
根据注释的属性使用 Spring”。在此上下文中,
“initialization” 是指新实例化的对象(例如,实例化的对象
使用new
运算符)以及Serializable
正在经历的对象
反序列化(例如,通过 readResolve())。
上一段中的关键词之一是“本质上”。在大多数情况下,
“After returning from the initialization of a new object” 的确切语义是
好。在这种情况下,“初始化后”意味着依赖项是
在构造对象后注入。这意味着依赖项
不能在类的构造函数主体中使用。如果您希望
依赖项,以便在构造函数主体运行之前注入,因此
,您需要在
您可以找到有关各种切入点的语言语义的更多信息 在 AspectJ 编程指南的这个附录中键入 AspectJ。 |
为此,必须用 AspectJ weaver 编织带注释的类型。您可以
使用构建时 Ant 或 Maven 任务来执行此作(例如,参见 AspectJ 开发
Environment Guide)或加载时编织(参见 Spring 框架中使用 AspectJ 的加载时编织)。这AnnotationBeanConfigurerAspect
本身需要由 Spring 进行配置(为了获得
对用于配置新对象的 Bean Factory 的引用)。如果你
使用基于 Java 的配置,您可以添加@EnableSpringConfigured
到任何@Configuration
类,如下所示:
-
Java
-
Kotlin
@Configuration
@EnableSpringConfigured
public class AppConfig {
}
@Configuration
@EnableSpringConfigured
class AppConfig {
}
如果您更喜欢基于 XML 的配置,则 Springcontext
Namespace定义一个方便的context:spring-configured
元素,您可以按如下方式使用:
<context:spring-configured/>
的实例@Configurable
在配置 aspect 之前创建的对象
导致向调试日志发出一条消息,并且没有配置
对象正在发生。一个例子可能是 Spring 配置中的一个 bean,它创建
domain 对象。在这种情况下,您可以使用depends-on
bean 属性手动指定 bean 依赖于
configuration 方面。以下示例演示如何使用depends-on
属性:
<bean id="myService"
class="com.xyz.service.MyService"
depends-on="org.springframework.beans.factory.aspectj.AnnotationBeanConfigurerAspect">
<!-- ... -->
</bean>
不激活@Configurable 通过 Bean Configurer 方面进行处理,除非你
真的意味着在运行时依赖它的语义。特别是,请确保您这样做
不使用@Configurable 在注册为常规 Spring bean 的 bean 类上
与容器一起。这样做会导致双重初始化,一次通过
容器和一次通过方面。 |
单元测试@Configurable
对象
的目标之一@Configurable
支持是启用独立的单元测试
的域对象,而没有与硬编码查找相关的困难。
如果@Configurable
类型没有被 AspectJ 编织,注解没有影响
在单元测试期间。您可以在
测试并照常进行。如果@Configurable
类型已经被 AspectJ 编织了,
您仍然可以像往常一样在容器外部进行单元测试,但您会看到一条警告
message@Configurable
对象,表示它具有
未由 Spring 配置。
使用多个应用程序上下文
这AnnotationBeanConfigurerAspect
用于实现@Configurable
支持
是 AspectJ 单例 aspect。单例 aspect 的 scope 与 scope 相同
之static
members:每个ClassLoader
定义类型。
这意味着,如果在同一ClassLoader
层次结构,您需要考虑在何处定义@EnableSpringConfigured
bean 和
放置位置spring-aspects.jar
在 Classpath 上。
考虑一个典型的 Spring Web 应用程序配置,该配置具有共享的父应用程序
定义常见业务服务的上下文,支持这些服务所需的一切,
以及每个 servlet 的一个子应用程序上下文(其中包含特定于
添加到该 Servlet 中)。所有这些上下文都在同一ClassLoader
等级制度
因此,AnnotationBeanConfigurerAspect
只能保存对其中一个的引用。
在这种情况下,我们建议定义@EnableSpringConfigured
共享的 bean
(父)应用程序上下文。这定义了你可能想要的服务
inject 到 domain objects 中。结果是您无法配置域对象
引用子(特定于 servlet)上下文中定义的 bean,方法是使用
@Configurable机制(这可能不是你想做的事情)。
在同一容器中部署多个 Web 应用程序时,请确保每个
Web 应用程序将类型加载到spring-aspects.jar
通过使用自己的ClassLoader
(例如,通过将spring-aspects.jar
在WEB-INF/lib
).如果spring-aspects.jar
仅添加到容器范围的 Classpath 中(因此由共享父级加载ClassLoader
),则所有 Web 应用程序共享相同的 aspect 实例(可能是
不是你想要的)。
AspectJ 的其他 Spring 方面
除了@Configurable
方面spring-aspects.jar
包含一个 AspectJ
方面,您可以使用它来驱动 Spring 的类型和方法的事务管理
用@Transactional
注解。这主要适用于满足以下条件的用户
希望在 Spring 容器之外使用 Spring Framework 的事务支持。
解释@Transactional
annotations 是AnnotationTransactionAspect
.当您使用此 aspect 时,您必须注释
implementation 类(或该类中的方法,或两者),而不是接口(如果
any) 实现。AspectJ 遵循 Java 的规则,即
接口不是继承的。
一个@Transactional
类的注解指定了
类中任何公共作的执行。
一个@Transactional
类中方法的注释将覆盖默认值
类注解(如果存在)给出的交易语义。任何
visibility 可以被注释,包括 private methods。对非公共方法进行注解
directly 是获取执行此类方法的事务划分的唯一方法。
从 Spring Framework 4.2 开始,spring-aspects 提供了一个类似的方面,该方面提供了
与标准完全相同的功能jakarta.transaction.Transactional 注解。检查JtaAnnotationTransactionAspect 了解更多详情。 |
对于想要使用 Spring 配置和事务的 AspectJ 程序员
管理支持,但不想(或不能)使用注解,spring-aspects.jar
还包含abstract
您可以扩展的 aspects 以提供您自己的切入点
定义。请参阅AbstractBeanConfigurerAspect
和AbstractTransactionAspect
aspects 了解更多信息。例如,以下
Excerpt 展示了如何编写一个 aspect 来配置对象的所有实例
使用与
完全限定的类名:
public aspect DomainObjectConfiguration extends AbstractBeanConfigurerAspect {
public DomainObjectConfiguration() {
setBeanWiringInfoResolver(new ClassNameBeanWiringInfoResolver());
}
// the creation of a new bean (any object in the domain model)
protected pointcut beanCreation(Object beanInstance) :
initialization(new(..)) &&
CommonPointcuts.inDomainModel() &&
this(beanInstance);
}
使用 Spring IoC 配置 AspectJ 方面
当你在 Spring 应用程序中使用 AspectJ 方面时,很自然地需要和
期望能够使用 Spring 配置这些方面。AspectJ 运行时本身是
负责 aspect 的创建,以及配置 AspectJ 创建的
方面依赖于 AspectJ 实例化模型(per-xxx
子句)
由 aspect 使用。
大多数 AspectJ 方面都是单例方面。这些的配置
方面很容易。您可以创建一个引用 aspect 类型的 bean 定义
normal 并包含factory-method="aspectOf"
bean 属性。这可确保
Spring 通过向 AspectJ 请求 aspect 实例来获取它,而不是尝试创建
实例本身。以下示例演示如何使用factory-method="aspectOf"
属性:
<bean id="profiler" class="com.xyz.profiler.Profiler"
factory-method="aspectOf"> (1)
<property name="profilingStrategy" ref="jamonProfilingStrategy"/>
</bean>
1 | 请注意factory-method="aspectOf" 属性 |
非单例方面更难配置。但是,可以通过以下方式执行此作
创建原型 Bean 定义并使用@Configurable
支持spring-aspects.jar
配置 aspect 实例,一旦它们具有由
AspectJ 运行时。
如果你有一些 @AspectJ 方面你想用 AspectJ 编织(例如,
对域模型类型使用加载时编织)和其他所需的@AspectJ方面
与 Spring AOP 一起使用,并且这些方面都在 Spring 中配置,那么
需要告诉 Spring AOP @AspectJ 自动代理支持是
配置中定义的@AspectJ方面都应用于自动代理。您可以
通过使用一个或多个<include/>
元素中<aop:aspectj-autoproxy/>
声明。每<include/>
元素指定名称模式,并且只有
与至少一个模式匹配的名称用于 Spring AOP 自动代理
配置。以下示例演示如何使用<include/>
元素:
<aop:aspectj-autoproxy>
<aop:include name="thisBean"/>
<aop:include name="thatBean"/>
</aop:aspectj-autoproxy>
不要被<aop:aspectj-autoproxy/> 元素。使用
导致创建 Spring AOP 代理。aspect 的 @AspectJ 样式
声明,但不涉及 AspectJ 运行时。 |
在 Spring 框架中使用 AspectJ 进行加载时编织
加载时编织 (LTW) 是指将 AspectJ 方面编织成 应用程序的类文件,因为它们正在加载到 Java 虚拟机 (JVM) 中。 本节的重点是在 Spring 框架。本节不是 LTW 的一般介绍。有关完整详细信息 LTW 的细节和仅使用 AspectJ 配置 LTW(Spring 不是 involved 的 lw),参见 AspectJ 的 LTW 部分 开发环境指南。
Spring 框架为 AspectJ LTW 带来的价值在于支持很多
对编织过程进行更精细的控制。'Vanilla' AspectJ LTW 通过使用
Java (5+) 代理,在启动
JVM 的 JVM 中。因此,它是一个 JVM 范围的设置,在某些情况下可能很好,但通常是一个
有点太粗糙了。启用 Spring 的 LTW 允许您在
每-ClassLoader
基础,它更细粒度,并且可以使
在“single-JVM-multiple-application”环境中(例如在典型的
应用程序服务器环境)。
此外,在某些环境中,此支持支持
加载时编织,而无需对 Application Server 的启动进行任何修改
需要添加的脚本-javaagent:path/to/aspectjweaver.jar
或(正如我们所描述的
本节稍后部分)-javaagent:path/to/spring-instrument.jar
.开发人员配置
应用程序上下文,用于启用加载时编织,而不是依赖管理员
通常负责部署配置(如 Launch Script)的人员。
现在销售宣传已经结束,让我们首先看一个 AspectJ 的快速示例 LTW,后跟有关 例。有关完整示例,请参阅基于 Spring Framework 的 Petclinic 示例应用程序。
第一个例子
假设您是一名应用程序开发人员,其任务是诊断 系统中某些性能问题的原因。而不是分解 profiling 工具中,我们将打开一个简单的性能分析方面,它允许我们 快速获取一些性能指标。然后,我们可以应用更细粒度的分析 工具复制到该特定区域。
此处显示的示例使用 XML 配置。您还可以配置和
将 @AspectJ 与 Java 配置结合使用。具体来说,您可以使用@EnableLoadTimeWeaving 注解作为<context:load-time-weaver/> (有关详细信息,请参见下文)。 |
下面的示例显示了性能分析方面,这并不花哨。 它是一个基于时间的分析器,它使用 @AspectJ 样式的 aspect 声明:
-
Java
-
Kotlin
package com.xyz;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.util.StopWatch;
import org.springframework.core.annotation.Order;
@Aspect
public class ProfilingAspect {
@Around("methodsToBeProfiled()")
public Object profile(ProceedingJoinPoint pjp) throws Throwable {
StopWatch sw = new StopWatch(getClass().getSimpleName());
try {
sw.start(pjp.getSignature().getName());
return pjp.proceed();
} finally {
sw.stop();
System.out.println(sw.prettyPrint());
}
}
@Pointcut("execution(public * com.xyz..*.*(..))")
public void methodsToBeProfiled(){}
}
package com.xyz
import org.aspectj.lang.ProceedingJoinPoint
import org.aspectj.lang.annotation.Aspect
import org.aspectj.lang.annotation.Around
import org.aspectj.lang.annotation.Pointcut
import org.springframework.util.StopWatch
import org.springframework.core.annotation.Order
@Aspect
class ProfilingAspect {
@Around("methodsToBeProfiled()")
fun profile(pjp: ProceedingJoinPoint): Any? {
val sw = StopWatch(javaClass.simpleName)
try {
sw.start(pjp.getSignature().getName())
return pjp.proceed()
} finally {
sw.stop()
println(sw.prettyPrint())
}
}
@Pointcut("execution(public * com.xyz..*.*(..))")
fun methodsToBeProfiled() {
}
}
我们还需要创建一个META-INF/aop.xml
文件,以通知 AspectJ weaver
我们想编织我们的ProfilingAspect
进入我们的课程。这个文件约定,即
Java 类路径上存在一个名为META-INF/aop.xml
是
标准 AspectJ.以下示例显示了aop.xml
文件:
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "https://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<weaver>
<!-- only weave classes in our application-specific packages and sub-packages -->
<include within="com.xyz..*"/>
</weaver>
<aspects>
<!-- weave in just this aspect -->
<aspect name="com.xyz.ProfilingAspect"/>
</aspects>
</aspectj>
建议只编织特定的类(通常是
application 软件包,如aop.xml 示例)按顺序
以避免副作用,例如 AspectJ 转储文件和警告。
从效率的角度来看,这也是一个最佳实践。 |
现在我们可以继续进行配置的 Spring 特定部分。我们需要
要配置LoadTimeWeaver
(稍后解释)。此加载时 weaver 是
essential 组件,负责将 aspect 配置编织到一个 OR
更多META-INF/aop.xml
文件复制到应用程序中的类中。好处
问题是它不需要很多配置(还有更多
选项,但稍后会详细介绍),如
以下示例:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- a service object; we will be profiling its methods -->
<bean id="entitlementCalculationService"
class="com.xyz.StubEntitlementCalculationService"/>
<!-- this switches on the load-time weaving -->
<context:load-time-weaver/>
</beans>
现在,所有必需的工件(方面、META-INF/aop.xml
文件和 Spring 配置)就位,我们可以创建以下内容
driver 类替换为main(..)
演示 LTW作的方法:
-
Java
-
Kotlin
package com.xyz;
// imports
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
EntitlementCalculationService service =
ctx.getBean(EntitlementCalculationService.class);
// the profiling aspect is 'woven' around this method execution
service.calculateEntitlement();
}
}
package com.xyz
// imports
fun main() {
val ctx = ClassPathXmlApplicationContext("beans.xml")
val service = ctx.getBean(EntitlementCalculationService.class)
// the profiling aspect is 'woven' around this method execution
service.calculateEntitlement()
}
我们还有最后一件事要做。本节的引言确实说过,人们可以
有选择地在 per- 上打开 LTWClassLoader
basis 替换为 Spring,这是真的。
但是,在此示例中,我们使用 Java 代理(随 Spring 提供)来开启 LTW。
我们使用以下命令运行Main
类:
java -javaagent:C:/projects/xyz/lib/spring-instrument.jar com.xyz.Main
这-javaagent
是用于指定和启用代理的标志
检测在 JVM 上运行的程序。Spring Framework 附带了这样一个
agent 中,InstrumentationSavingAgent
,它打包在spring-instrument.jar
作为-javaagent
argument 在
前面的示例。
执行Main
program 类似于下一个示例。
(我引入了一个Thread.sleep(..)
语句添加到calculateEntitlement()
实现,以便分析器实际捕获 0 以外的内容
毫秒(01234
milliseconds 不是 AOP 引入的开销)。
下面的清单显示了我们在运行 profiler 时得到的输出:
Calculating entitlement StopWatch 'ProfilingAspect': running time (millis) = 1234 ------ ----- ---------------------------- ms % Task name ------ ----- ---------------------------- 01234 100% calculateEntitlement
由于这个 LTW 是通过使用成熟的 AspectJ 来实现的,因此我们不仅限于提供建议
春豆。以下对Main
program 生成相同的
结果:
-
Java
-
Kotlin
package com.xyz;
// imports
public class Main {
public static void main(String[] args) {
new ClassPathXmlApplicationContext("beans.xml");
EntitlementCalculationService service =
new StubEntitlementCalculationService();
// the profiling aspect will be 'woven' around this method execution
service.calculateEntitlement();
}
}
package com.xyz
// imports
fun main(args: Array<String>) {
ClassPathXmlApplicationContext("beans.xml")
val service = StubEntitlementCalculationService()
// the profiling aspect will be 'woven' around this method execution
service.calculateEntitlement()
}
请注意,在前面的程序中,我们如何引导 Spring 容器并
然后创建一个StubEntitlementCalculationService
完全在外面
Spring 的上下文。分析建议仍然被编织在一起。
诚然,这个例子很简单。但是,Spring 中 LTW 支持的基础知识 在前面的示例中都已介绍过,本节的其余部分将解释 每个配置和用法背后的 “为什么” 都很详细。
这ProfilingAspect 此示例中使用的可能是基本的,但它非常有用。它是一个
开发人员可以在开发过程中使用的开发时方面的一个很好的示例
然后轻松地从正在部署的应用程序的构建中排除
进入 UAT 或生产。 |
方面
你在 LTW 中使用的 aspect 必须是 AspectJ 方面。您可以将它们写入 要么是 AspectJ 语言本身,要么你可以用 @AspectJ 风格编写你的 Aspects。 因此,你的 aspect 都是有效的 AspectJ 和 Spring AOP 方面。 此外,编译后的 aspect 类需要在 Classpath 上可用。
META-INF/aop.xml
AspectJ LTW 基础结构通过使用一个或多个META-INF/aop.xml
位于 Java 类路径上的文件(直接或在 jar 文件中)。
例如:
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "https://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<weaver>
<!-- only weave classes in our application-specific packages and sub-packages -->
<include within="com.xyz..*"/>
</weaver>
</aspectj>
建议只编织特定的类(通常是
application 软件包,如aop.xml 示例)按顺序
以避免副作用,例如 AspectJ 转储文件和警告。
从效率的角度来看,这也是一个最佳实践。 |
此文件的结构和内容在 AspectJ 参考的 LTW 部分中有详细说明
文档。因为aop.xml
文件是 100% 的 AspectJ,我们在这里不再进一步描述它。
必需的库 (JAR)
至少,您需要以下库才能使用 Spring Framework 的支持 对于 AspectJ LTW:
-
spring-aop.jar
-
aspectjweaver.jar
如果使用 Spring 提供的代理来启用 instrumentation ,则还需要:
-
spring-instrument.jar
Spring 配置
Spring 的 LTW 支持中的关键组件是LoadTimeWeaver
界面(在org.springframework.instrument.classloading
包)和众多实现
的 Spring 发行版。一个LoadTimeWeaver
负责
添加一个或多个java.lang.instrument.ClassFileTransformers
更改为ClassLoader
在
runtime 的 runtime,它为各种有趣的应用程序打开了大门,其中之一就是
恰好是 aspects 的 LTW。
如果您不熟悉运行时类文件转换的概念,请参阅
javadoc API 文档java.lang.instrument 包,然后再继续。
虽然该文档并不全面,但至少您可以看到关键界面
和 classes (供你阅读本节时参考)。 |
配置LoadTimeWeaver
对于特定的ApplicationContext
可以像
添加一行。(请注意,您几乎肯定需要使用ApplicationContext
作为您的 Spring 容器 — 通常为BeanFactory
莫
足够了,因为 LTW 支持使用BeanFactoryPostProcessors
.)
要启用 Spring 框架的 LTW 支持,你需要配置一个LoadTimeWeaver
,
这通常通过使用@EnableLoadTimeWeaving
注解,如下所示:
-
Java
-
Kotlin
@Configuration
@EnableLoadTimeWeaving
public class AppConfig {
}
@Configuration
@EnableLoadTimeWeaving
class AppConfig {
}
或者,如果您更喜欢基于 XML 的配置,请使用<context:load-time-weaver/>
元素。请注意,该元素在context
Namespace。以下示例演示如何使用<context:load-time-weaver/>
:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:load-time-weaver/>
</beans>
前面的配置会自动定义并注册一些特定于 LTW 的
基础设施 Bean,例如LoadTimeWeaver
以及一个AspectJWeavingEnabler
给你的。
默认的LoadTimeWeaver
是DefaultContextLoadTimeWeaver
类,该类会尝试
以装饰自动检测到的LoadTimeWeaver
.的确切类型LoadTimeWeaver
即 “Automatically detected” 取决于您的运行时环境。
下表总结了各种LoadTimeWeaver
实现:
运行时环境 | LoadTimeWeaver 实现 |
---|---|
在 Apache Tomcat 中运行 |
|
在 GlassFish 中运行(仅限于 EAR 部署) |
|
|
|
JVM 始于 Spring |
|
Fallback,期望底层 ClassLoader 遵循通用约定
(即 |
|
请注意,该表仅列出了LoadTimeWeavers
当您
使用DefaultContextLoadTimeWeaver
.您可以准确指定LoadTimeWeaver
implementation 来使用。
要指定特定的LoadTimeWeaver
使用 Java 配置,实现LoadTimeWeavingConfigurer
接口并覆盖getLoadTimeWeaver()
方法。
以下示例指定ReflectiveLoadTimeWeaver
:
-
Java
-
Kotlin
@Configuration
@EnableLoadTimeWeaving
public class AppConfig implements LoadTimeWeavingConfigurer {
@Override
public LoadTimeWeaver getLoadTimeWeaver() {
return new ReflectiveLoadTimeWeaver();
}
}
@Configuration
@EnableLoadTimeWeaving
class AppConfig : LoadTimeWeavingConfigurer {
override fun getLoadTimeWeaver(): LoadTimeWeaver {
return ReflectiveLoadTimeWeaver()
}
}
如果使用基于 XML 的配置,则可以指定完全限定的类名
作为weaver-class
属性<context:load-time-weaver/>
元素。同样,以下示例指定了ReflectiveLoadTimeWeaver
:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:load-time-weaver
weaver-class="org.springframework.instrument.classloading.ReflectiveLoadTimeWeaver"/>
</beans>
这LoadTimeWeaver
由配置定义和注册的
使用众所周知的名称从 Spring 容器中检索loadTimeWeaver
.
请记住,LoadTimeWeaver
仅作为 Spring 的 LTW 的机制存在
infrastructure 添加一个或多个ClassFileTransformers
.实际的ClassFileTransformer
LTW 是ClassPreProcessorAgentAdapter
(来自
这org.aspectj.weaver.loadtime
package) 类。请参阅ClassPreProcessorAgentAdapter
类了解更多详细信息,因为具体作
编织实际发生超出了本文档的范围。
配置还有一个最后一个属性需要讨论:aspectjWeaving
属性(或aspectj-weaving
如果使用 XML)。此属性控制 LTW
是否启用。它接受三个可能的值之一,默认值为autodetect
如果该属性不存在。下表总结了这三种
可能的值:
注释值 | XML 值 | 解释 |
---|---|---|
|
|
AspectJ 编织已打开,并且 aspect 会根据需要在 load 时进行编织。 |
|
|
LTW 已关闭。在加载时没有编织任何方面。 |
|
|
如果 Spring LTW 基础结构可以找到至少一个 |
特定于环境的配置
最后一部分包含您需要的任何其他设置和配置 当您在应用程序服务器和 Web 等环境中使用 Spring 的 LTW 支持时 器皿。
Tomcat、JBoss、WildFly
Tomcat 和 JBoss/WildFly 提供了一个通用应用程序ClassLoader
能够本地
仪表。Spring 的原生 LTW 可以利用这些 ClassLoader 实现
提供 AspectJ 编织。
如前所述,您只需启用 load-time weaving。
具体来说,您无需修改 JVM 启动脚本即可将-javaagent:path/to/spring-instrument.jar
.
请注意,在 JBoss 上,你可能需要禁用应用服务器扫描以防止它
在应用程序实际启动之前加载类。快速解决方法是将
将名为WEB-INF/jboss-scanning.xml
包含以下内容:
<scanning xmlns="urn:jboss:scanning:1.0"/>
通用 Java 应用程序
当在 不支持的环境中需要类插桩时
特定LoadTimeWeaver
实现中,JVM 代理是通用的解决方案。
对于这种情况, Spring 提供了InstrumentationLoadTimeWeaver
这需要
特定于 Spring(但非常通用)的 JVM 代理,spring-instrument.jar
、自动检测
按普通@EnableLoadTimeWeaving
和<context:load-time-weaver/>
设置。
要使用它,您必须通过提供 Spring 代理来启动虚拟机 以下 JVM 选项:
-javaagent:/path/to/spring-instrument.jar
请注意,这需要修改 JVM 启动脚本,这可能会阻止您 在应用程序服务器环境中使用它(取决于您的服务器和 作策略)。也就是说,对于每个 JVM 一个应用程序的部署,例如独立的 Spring Boot 应用程序,您通常在任何情况下都可以控制整个 JVM 设置。