gpt4 book ai didi

spring - 当尝试从非 Artefact Groovy 文件中的静态方法访问服务时,在静态作用域中发现了明显的变量 'serviceName'

转载 作者:行者123 更新时间:2023-12-02 09:35:41 24 4
gpt4 key购买 nike

在 Grails 3 应用程序中,我使用 Spring Beans 将两个服务注入(inject)到位于 src/main/groovy/demo/Menu.groovy 的非工件 Groovy 文件中。然后,我尝试从 Controller 调用创建菜单静态方法,但是当从静态方法调用注入(inject)服务的方法时出现错误

菜单类如下所示

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 Data Services并在resource.groovy文件中使用Spring bean注入(inject)

beans = {    
menu(demo.Menu)
}

当我调用 create Menu 静态方法时从 Controller 操作

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< 访问该字段 方法。

由于您已经创建了 Menu bean,因此只需尝试注入(inject)它,而不是使 create 方法静态:

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 管理创建和注入(inject)其他服务?

关于spring - 当尝试从非 Artefact Groovy 文件中的静态方法访问服务时,在静态作用域中发现了明显的变量 'serviceName',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46960479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com