Java 使用 Beanshell 以干净的代码访问字段和对象
Java with Beanshell to access fields and object with clean code
1).我知道如何从我的问题 . However, it is not so clean way to implement as I need to first set the java variable in beanshell and then I can use it. However, in 中访问 beanshell 中的 java 字段和对象,它提供了在 beanshell 中使用地图的非常干净的方式,类似于我们在 java 中所做的,但是 JMeter 已经开发它是已知库 (class),可帮助访问地图的 get/put 方法。我想在beanshell中实现类似的访问Map的方式。
我已经检查了 JMeter 以获取更多信息,我想知道,我创建了用户定义变量 temp 并赋值错误,现在在 BSF 过程中我只写了一行 vars.put('Name','temp Value') 并且它更新了临时变量的值。所以,问题是我没有创建 JMeterVariables 对象变量,但 beanshell 仍然允许更新地图中的值,而无需设置任何值,如您的答案中所述。我想知道这是如何工作的,需要更多深度信息。
2).我在 java 中创建了自己的 class,在 beanshell 中我导入了这个 class 但它给出了 Command not found: BSClass()
下面是整个代码
Java class
package test;
public class BSClass {
public void BSCMethod(){
System.out.println("I am from BSClass method BSCMethod");
}
}
sample.bsh
import test.BSClass;
c=BSClass();
c.BSCMethod();
print("I am from BeanShell Script");
正在调用 sample.bsh 文件 java class
package test;
import java.io.FileNotFoundException;
import java.io.IOException;
import bsh.*;
public class DynamicVariable {
public static void main(String[] args) throws FileNotFoundException, IOException, EvalError {
new bsh.Interpreter().source("\src\test\sample.bsh");
}
}
注:
- 我在JMeter中不需要帮助,它是在核心java和beanshell中使用。
- 所有文件都在我的项目中。
- BSClass.class在我项目的bin文件夹下
非常感谢您的意见
在 Beanshell 中,您可以添加任何您想要的对象,包括地图
在 JMeter 中,JMeterVariables
是 Map 的特殊实现,它在求值之前添加到 Beanshell Interpreter 中,还添加了特殊的对象,如 JMeterContext
,其中甚至包括 JMeterVariables
。代码:
JMeterContext jmctx = JMeterContextService.getContext();
JMeterVariables vars = jmctx.getVariables();
try {
bshInterpreter.set("ctx", jmctx);//$NON-NLS-1$
bshInterpreter.set("Label", getName()); //$NON-NLS-1$
bshInterpreter.set("prev", jmctx.getPreviousResult());//$NON-NLS-1$
bshInterpreter.set("props", JMeterUtils.getJMeterProperties());
bshInterpreter.set("vars", vars);//$NON-NLS-1$
在你使用地图的情况下,你可以像评论中描述的那样做类似的事情:
bshInterpreter.set("myMap", javaMyMapObject);"
然后在 Beanshell 中从映射中获取特定的键:
myMap.get("aField");
要创建 class,您应该使用 new
关键字,调用:
c= new BSClass();
而不是c=BSClass();
如果您创建自己的 class,Class 应该在相关包的 jar 中。
jar 应该位于 lib 文件夹中而不是 bin 文件夹中,参见 JMeter's getting started:
Any jar file in such a directory will be automatically included in
user.classpath, jar files in sub directories are ignored. The given
value is in addition to any jars found in the lib directory. All
entries will be added to the class path of the system class loader and
also to the path of the JMeter internal loader.
1).我知道如何从我的问题
我已经检查了 JMeter 以获取更多信息,我想知道,我创建了用户定义变量 temp 并赋值错误,现在在 BSF 过程中我只写了一行 vars.put('Name','temp Value') 并且它更新了临时变量的值。所以,问题是我没有创建 JMeterVariables 对象变量,但 beanshell 仍然允许更新地图中的值,而无需设置任何值,如您的答案中所述。我想知道这是如何工作的,需要更多深度信息。
2).我在 java 中创建了自己的 class,在 beanshell 中我导入了这个 class 但它给出了 Command not found: BSClass()
下面是整个代码
Java class
package test;
public class BSClass {
public void BSCMethod(){
System.out.println("I am from BSClass method BSCMethod");
}
}
sample.bsh
import test.BSClass;
c=BSClass();
c.BSCMethod();
print("I am from BeanShell Script");
正在调用 sample.bsh 文件 java class
package test;
import java.io.FileNotFoundException;
import java.io.IOException;
import bsh.*;
public class DynamicVariable {
public static void main(String[] args) throws FileNotFoundException, IOException, EvalError {
new bsh.Interpreter().source("\src\test\sample.bsh");
}
}
注:
- 我在JMeter中不需要帮助,它是在核心java和beanshell中使用。
- 所有文件都在我的项目中。
- BSClass.class在我项目的bin文件夹下
非常感谢您的意见
在 Beanshell 中,您可以添加任何您想要的对象,包括地图
在 JMeter 中,JMeterVariables
是 Map 的特殊实现,它在求值之前添加到 Beanshell Interpreter 中,还添加了特殊的对象,如 JMeterContext
,其中甚至包括 JMeterVariables
。代码:
JMeterContext jmctx = JMeterContextService.getContext();
JMeterVariables vars = jmctx.getVariables();
try {
bshInterpreter.set("ctx", jmctx);//$NON-NLS-1$
bshInterpreter.set("Label", getName()); //$NON-NLS-1$
bshInterpreter.set("prev", jmctx.getPreviousResult());//$NON-NLS-1$
bshInterpreter.set("props", JMeterUtils.getJMeterProperties());
bshInterpreter.set("vars", vars);//$NON-NLS-1$
在你使用地图的情况下,你可以像评论中描述的那样做类似的事情:
bshInterpreter.set("myMap", javaMyMapObject);"
然后在 Beanshell 中从映射中获取特定的键:
myMap.get("aField");
要创建 class,您应该使用 new
关键字,调用:
c= new BSClass();
而不是c=BSClass();
如果您创建自己的 class,Class 应该在相关包的 jar 中。
jar 应该位于 lib 文件夹中而不是 bin 文件夹中,参见 JMeter's getting started:
Any jar file in such a directory will be automatically included in user.classpath, jar files in sub directories are ignored. The given value is in addition to any jars found in the lib directory. All entries will be added to the class path of the system class loader and also to the path of the JMeter internal loader.