見かけの変数「serviceName」が静的スコープで見つかりました...アーティファクト以外のGroovyファイルの静的メソッドからサービスにアクセスしようとしたとき

マリオ

Grails 3アプリでは、Spring Beansを使用して、にあるアーティファクトではないGroovyファイルに2つのサービスを挿入していsrc/main/groovy/demo/Menu.groovyます。次に、コントローラーからcreate Menu静的メソッドを呼び出そうとしましたが、静的メソッドから注入されたサービスのメソッドを呼び出すとエラーが発生します

Menuクラスは次のようになります

package demo

import org.springframework.beans.factory.annotation.Autowired
import demo.CategoryService
import demo.ItemService

class Menu {

    @Autowired
    CategoryService categoryService

    @Autowired
    ItemService itemService

    List<Category> categoryList
    List<Item> itemList

    public static final Menu create(final String categoryName) {
        List<Category> categoryList = categoryService.listByEnabled(true)

        new Menu (
            categoryList: categoryList,
            itemList: itemService.listByCategoryName(categoryName ?: categoryList[0].name)
        )
    }
}

CategoryServiceとItemServiceはどちらもGORMデータサービスであり、resource.groovyファイルでSpringBeanを使用して注入されます

beans = {    
    menu(demo.Menu)
}

createMenu静的メソッドを呼び出すときのコントローラーアクションから

class MenuController {

    def menu(final String categoryName) {
        respond Menu.create(categoryName)
    }
}

次のエラーが発生します。

/home/desktop/pos/src/main/groovy/demo/Menu.groovy: 17: Apparent variable 'categoryService' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method 'categoryService' but left out brackets in a place not allowed by the grammar.
 @ line 17, column 39.
           List<Category> categoryList = categoryService.listByEnabled(true)

そしてitemServiceについても同じです

/home/desktop/pos/src/main/groovy/demo/Menu.groovy: 21: Apparent variable 'itemService' was found in a static scope but doesn't refer to a local variable, static field or class. Possible causes:
You attempted to reference a variable in the binding or an instance variable from a static context.
You misspelled a classname or statically imported field. Please check the spelling.
You attempted to use a method 'itemService' but left out brackets in a place not allowed by the grammar.
 @ line 21, column 23.
               itemList: itemService.listByCategoryName(categoryName ?: categoryList[0].name)

御時間ありがとうございます

ブライアン

メソッドからアクセスできないcategoryServiceインスタンスフィールド(static修飾子なしとして定義したため、エラーが発生していますstatic

すでにMenuBeanを作成しているのでcreateメソッドを作成する代わりに、それを注入してみてくださいstatic

class MenuController {

    def menu // Grails should inject it by bean name here

    def menu(final String categoryName) {
        respond menu.create(categoryName)
    }
}

次にstatic、宣言から削除します。

public final Menu create(final String categoryName) {

しかし、これは疑問を投げかけます、なぜMenu骨の折れるGrailsサービスを作ってみませんか?たとえば、それをgrails-app / servicesに移動し、Grailsに他のサービスの作成と注入を管理させますか?

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ