2

Groovy スクリプトを実行する Spring Controlled Grovoy Script Executor クラスがあります。

以下のようなもの

    final ClassLoader parent = getClass().getClassLoader();
    final GroovyClassLoader loader;

    loader = AccessController.doPrivileged(new PrivilegedAction<GroovyClassLoader>() {
        @Override
        public GroovyClassLoader run() {
            return new GroovyClassLoader(parent);
        }
    });


    this.groovyClass = loader.parseClass(" def returnSomthing() { return SpringControlledBean.action('Hello World') } ");
    final GroovyObject groovyObject = (GroovyObject) this.groovyClass.newInstance();
    final Object[] args = { };
    final Object result = groovyObject.invokeMethod("returnSomthing", args);

SpringControlledBeanをスクリプトに挿入することは可能ですか? おそらく自動配線を介して、またはSpringにクラスを作成させて、スクリプトが変更されるとクラスを再作成する必要があることを理解しますか?

クラスがクラスパスの一部であり、Java でビルドされた場合、Autowire は可能ですが、このスクリプトの内容は実行時に過去のものであるため、Spring が知るには静的ではありません。

4

1 に答える 1

1

AutowireCapableBeanFactoryクラスを として宣言することで取得できるのインスタンスが必要です。BeanFactoryAwareその後、 メソッド を呼び出すことができますautowireBean(existingBean)

例えば:

class MyBeanCreator implements BeanFactoryAware {

  private AutowireCapableBeanFactory beanFactory; //you need to add setter as well

  def foobar() {
    //your existing code....
    final GroovyObject groovyObject = (GroovyObject) this.groovyClass.newInstance();

    //Wire with Spring 
    beanFactory.autowireBean(groovyObject);

    //rest of your existing code...
  }

}

参照: http://static.springsource.org/spring/docs/3.0.x/api/org/springframework/beans/factory/config/AutowireCapableBeanFactory.html

于 2013-02-20T05:08:34.043 に答える