クラス GroovyBeanDefinitionReader

java.lang.ObjectSE
org.springframework.beans.factory.support.AbstractBeanDefinitionReader
org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader
実装されたすべてのインターフェース:
groovy.lang.GroovyObjectBeanDefinitionReaderEnvironmentCapable

public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader implements groovy.lang.GroovyObject
Spring Bean 定義用の Groovy ベースのリーダー: Groovy ビルダーに似ていますが、Spring 構成用の DSL です。

この Bean 定義リーダーは、XML Bean 定義ファイルも理解できるため、Groovy Bean 定義ファイルとのシームレスな混合とマッチングが可能になります。

通常は DefaultListableBeanFactory または GenericApplicationContext に適用されますが、任意の BeanDefinitionRegistry 実装に対して使用できます。

構文例

 import org.hibernate.SessionFactory
 import org.apache.commons.dbcp.BasicDataSource

 def reader = new GroovyBeanDefinitionReader(myApplicationContext)
 reader.beans {
     dataSource(BasicDataSource) {                  // <--- invokeMethod
         driverClassName = "org.hsqldb.jdbcDriver"
         url = "jdbc:hsqldb:mem:grailsDB"
         username = "sa"                            // <-- setProperty
         password = ""
         settings = [mynew:"setting"]
     }
     sessionFactory(SessionFactory) {
         dataSource = dataSource                    // <-- getProperty for retrieving references
     }
     myService(MyService) {
         nestedBean = { AnotherBean bean ->         // <-- setProperty with closure for nested bean
             dataSource = dataSource
         }
     }
 }

次のようなスクリプトを使用して、AbstractBeanDefinitionReader.loadBeanDefinitions(Resource...) または AbstractBeanDefinitionReader.loadBeanDefinitions(String...) メソッドを使用して、Groovy スクリプトで定義された Bean を含むリソースをロードすることもできます。

 import org.hibernate.SessionFactory
 import org.apache.commons.dbcp.BasicDataSource

 beans {
     dataSource(BasicDataSource) {
         driverClassName = "org.hsqldb.jdbcDriver"
         url = "jdbc:hsqldb:mem:grailsDB"
         username = "sa"
         password = ""
         settings = [mynew:"setting"]
     }
     sessionFactory(SessionFactory) {
         dataSource = dataSource
     }
     myService(MyService) {
         nestedBean = { AnotherBean bean ->
             dataSource = dataSource
         }
     }
 }
導入:
4.0
作成者:
Jeff Brown, Graeme Rocher, Juergen Hoeller, Sam Brannen
関連事項:
  • コンストラクターの詳細

    • GroovyBeanDefinitionReader

      public GroovyBeanDefinitionReader(BeanDefinitionRegistry registry)
      指定された BeanDefinitionRegistry の新しい GroovyBeanDefinitionReader を作成します。
      パラメーター:
      registry - Bean 定義をロードする BeanDefinitionRegistry 
    • GroovyBeanDefinitionReader

      public GroovyBeanDefinitionReader(XmlBeanDefinitionReader xmlBeanDefinitionReader)
      指定された XmlBeanDefinitionReader に基づいて新しい GroovyBeanDefinitionReader を作成し、Bean 定義を BeanDefinitionRegistry にロードし、Groovy DSL ロードをそれに委譲します。

      提供される XmlBeanDefinitionReader は、通常、XML 検証を無効にして事前構成する必要があります。

      パラメーター:
      xmlBeanDefinitionReader - XmlBeanDefinitionReader からレジストリを派生させ、Groovy DSL の読み込みを委譲する
  • メソッドの詳細

    • setMetaClass

      public void setMetaClass(groovy.lang.MetaClass metaClass)
      次で指定:
      インターフェース groovy.lang.GroovyObjectsetMetaClass 
    • getMetaClass

      public groovy.lang.MetaClass getMetaClass()
      次で指定:
      インターフェース groovy.lang.GroovyObjectgetMetaClass 
    • setBinding

      public void setBinding(groovy.lang.Binding binding)
      バインディング、つまり GroovyBeanDefinitionReader クロージャーのスコープで使用可能な Groovy 変数を設定します。
    • getBinding

      @Nullable public groovy.lang.Binding getBinding()
      Groovy 変数の指定されたバインディングがあれば、それを返します。
    • loadBeanDefinitions

      public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException
      指定された Groovy スクリプトまたは XML ファイルから Bean 定義をロードします。

      ".xml" ファイルは XML コンテンツとして解析されることに注意してください。他のすべての種類のリソースは、Groovy スクリプトとして解析されます。

      次で指定:
      インターフェース BeanDefinitionReaderloadBeanDefinitions 
      パラメーター:
      resource - Groovy スクリプトまたは XML ファイルのリソース記述子
      戻り値:
      見つかった Bean 定義の数
      例外:
      BeanDefinitionStoreException - ロードまたは解析エラーの場合
    • loadBeanDefinitions

      public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException
      指定された Groovy スクリプトまたは XML ファイルから Bean 定義をロードします。

      ".xml" ファイルは XML コンテンツとして解析されることに注意してください。他のすべての種類のリソースは、Groovy スクリプトとして解析されます。

      パラメーター:
      encodedResource - Groovy スクリプトまたは XML ファイルのリソース記述子。ファイルの解析に使用するエンコーディングを指定できます
      戻り値:
      見つかった Bean 定義の数
      例外:
      BeanDefinitionStoreException - ロードまたは解析エラーの場合
    • beans

      public GroovyBeanDefinitionReader beans(groovy.lang.Closure<?> closure)
      指定されたブロックまたはクロージャの Bean のセットを定義します。
      パラメーター:
      closure - ブロックまたは閉鎖
      戻り値:
      この GroovyBeanDefinitionReader インスタンス
    • bean

      public GenericBeanDefinition bean(ClassSE<?> type)
      内部 Bean 定義を定義します。
      パラメーター:
      type - Bean 型
      戻り値:
      Bean 定義
    • bean

      public AbstractBeanDefinition bean(ClassSE<?> type, ObjectSE... args)
      内部 Bean 定義を定義します。
      パラメーター:
      type - Bean 型
      args - コンストラクターの引数とクロージャー構成子
      戻り値:
      Bean 定義
    • xmlns

      public void xmlns(MapSE<StringSE,StringSE> definition)
      使用する Spring XML 名前空間定義を定義します。
      パラメーター:
      definition - 名前空間の定義
    • importBeans

      public void importBeans(StringSE resourcePattern) throws IOExceptionSE
      XML または Groovy ソースから Spring Bean 定義を現在の Bean ビルダーインスタンスにインポートします。
      パラメーター:
      resourcePattern - リソースパターン
      例外:
      IOExceptionSE
    • invokeMethod

      public ObjectSE invokeMethod(StringSE name, ObjectSE arg)
      このメソッドは、メソッドの呼び出しをオーバーライドして、クラス引数を取る各メソッド名の Bean を作成します。
      次で指定:
      インターフェース groovy.lang.GroovyObjectinvokeMethod 
    • invokeBeanDefiningClosure

      protected GroovyBeanDefinitionReader invokeBeanDefiningClosure(groovy.lang.Closure<?> callable)
      メソッド引数がクロージャのみの場合、Bean 定義のセットです。
      パラメーター:
      callable - クロージャー引数
      戻り値:
      この GroovyBeanDefinitionReader インスタンス
    • resolveConstructorArguments

      protected ListSE<ObjectSE> resolveConstructorArguments(ObjectSE[] args, int start, int end)
    • setProperty

      public void setProperty(StringSE name, ObjectSE value)
      このメソッドは、GroovyBeanDefinitionReader のスコープ内のプロパティ設定をオーバーライドして、現在の Bean 定義にプロパティを設定します。
      次で指定:
      インターフェース groovy.lang.GroovyObjectsetProperty 
    • applyPropertyToBeanDefinition

      protected void applyPropertyToBeanDefinition(StringSE name, ObjectSE value)
    • getProperty

      @Nullable public ObjectSE getProperty(StringSE name)
      このメソッドは、GroovyBeanDefinitionReader のスコープ内のプロパティの取得をオーバーライドします。プロパティの取得は次のいずれかになります。
      • 変数が存在する場合、Bean ビルダーのバインディングから変数を取得します。
      • 特定の Bean が存在する場合は、その RuntimeBeanReference を取得します。
      • それ以外の場合は、GroovyBeanDefinitionReader 自体からプロパティを解決する MetaClass.getProperty にデリゲートするだけです
      次で指定:
      インターフェース groovy.lang.GroovyObjectgetProperty