beanShell不错

beanShell不错
key words : beanShell ,动态脚本

原来第一次是在osworkflow中听说过beanShell,最近又碰到,感觉还是挺有用的,比如对于我来说我就想有一些class文件拿过来直接调用看看,但有不想搭建一个麻烦的环境,而beanshell就可以满足我这个需求。

假设我欲调用的java为MyShit
package  com.app;

public   class  MyShit
{
    
static{
        System.out.println(
"this is in static blog");
    }

    
public static void main(String[] args){
        System.out.println(
"this is in main method : hello shit");
    }

    
public void shit(){
        System.out.println(
"hello shit!!!");
    }


    
public static void haha(){
        System.out.println(
"this is static method haha");
    }

}


javac ...
java ..

在dos中设置classpath,指向该class文件

set classpath=%classpath%;c:\beanshell

编写一个 bsh的bat文件,便于dos直接调用beanshell脚本
 
java bsh.Interpreter  % 1


编写一个test.bsh脚本
// mytest
import  com.app. * ;

print(
" hello,it's a beanShell test " );

List list  
=   new  ArrayList();
list.add(
" 111 " );
list.add(
" 222 " );
list.add(
" 333 " );

print(
" the list =  "   +  list);

Date date 
=   new  Date();
print(
" the date =  "   +  date);

// method test

add( a, b ) 
{
    
return a + b;
}


foo 
=  add( 1 2 );             //  3
print( " foo =  "   +  foo);
foo 
=  add( " Oh " "  baby " );    //  "Oh baby"
print( " foo =  "   +  foo);

// about object
foo()  {
    print(
"foo");
    x
=5;

    bar() 
{
        print(
"foo's method bar()");
    }


    
return this;
}


myfoo 
=  foo();     //  prints "foo"
print( myfoo.x );  //  prints "5"
myfoo.bar();       //  prints "bar"

MyShit shit 
=   new  MyShit();
shit.shit();
shit.main(
null );
print(
" do you shit " );

// MyShit.haha();


在dos下运行 bsh test.bsh就OK了

另外,beanshell作为动态脚本语言可以结合 spring2.0的新特性,你新写的业务类可以直接修改而不用重新发布,怎么样,是不是比较方便,详细操作请参考 Springframework 2.0 与 ZK 混合开发实例

试一试!

详细使用请参考 官方文档

你可能感兴趣的:(beanShell不错)