- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 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/
我正在使用VirtualTreeView与 OnMeasureItem 事件一起显示可变大小的行。问题在于,该事件似乎仅在绘制一行时才被调用(遵循虚拟范例)。但这会导致滚动条显示不正确。如果我滚动到底
Jooq 代码生成器在指定目录中创建了一些很棒的全局工件。但是有没有办法在它们上面添加一些前缀/后缀,这样名称就不会与实际实体发生冲突? 我一直在读this但没有任何希望:/ 我有一个包“实体”,里面
因此,Android 中的 WebView 硬件加速存在一个已知错误,例如:https://code.google.com/p/android/issues/detail?id=17352 禁用硬件加
我正在绘制一个带有几个圆角的简单矩形: UIBezierPath * p = [UIBezierPath bezierPathWithRoundedRect:outline
我目前正在开发针对服务器和 Android 设备的 SDK。我的可交付 JAR 是 JarJar'ed,因此可以防止客户端应用程序中发生任何依赖项泄漏(使用 maven-jarjar-plugin)。
我正在从以下格式转换: const int four_bytes_per_float = 4; const int eight_bits_per_byte = 8; _stereoGraphStrea
下面的类附加到 UIView 并在手指在屏幕上移动时绘制线条。然而,当从上到下从左到右快速移动手指时,绘图会在改变方向时暂时显示尖锐的边缘。这在设备和模拟器上都会发生。 导致此问题的原因是什么?如何在
在 Grails 3 应用程序中,我使用 Spring Beans 将两个服务注入(inject)到位于 src/main/groovy/demo/Menu.groovy 的非工件 Groovy 文件
我正在使用 thoughworks go 构建管道,如下所示: “测试”阶段从构建阶段获取工件,并在不同时期并行运行它的每个作业(单元测试、集成测试、验收测试、包)。但是,这些作业中的每一个都是一个
一些 Powermock/Mockito 文章提到了使用 PowerMock.mockStaticPartial 方法 - 唯一的问题是我在任何地方都找不到它! 有谁知道它存在于哪个 Artifact
WebApp 配置为从 ACR 获取工件作为 CI/CD 的一部分。与 ACR 工件相比,Web 应用程序似乎正在运行旧文件(通过将其从 ACR 拉到本地主机进行验证)。 已执行以下操作: 重新启动
我们的多项目构建具有以下结构: 根项目 \-项目1 \-项目2 \-build.gradle \-settings.gradle 当我从根项目目录运行 gradle build 时,project1(
我正在尝试将我的两个项目链接为一个。一个是用于收集数据的 REST API,另一个是用于创建用于控制另一个的 GUI 的 java swing 应用程序。我已经独立运行了它们,它们工作得很好。不过,当
我有一个非常奇怪的问题,我有一个基于项目的 nuget feed,一个管道发布到它,工作正常,然后另一个管道需要恢复使用此 nuget feed 的项目。 问题是我已遵循所有相关说明,例如: 确保构建
我已将我的应用程序从grails 2.5升级到grails 3.3.11。我已经按照所有步骤进行了,一切似乎都很好,但是当我运行该应用程序时。我遇到这个错误: 启动失败: /home/server-d
这个问题有很多,但没有一个有答案......当我调用这个方法时,我的控制台中弹出错误并且返回成功。造成这种情况的原因是什么? 使用 grails 3.1.8 Ambiguous method over
我被 Azure 管道卡住了。 我有一个现有的节点 SPA 项目,需要为每个环境(测试和生产)构建。我可以做到这一点,但在推送到 PROD 时需要手动执行步骤。我正在使用带有批准和检查的 Azure
我已经使用 Grails 工作了大约一个月,目前我们正在使用 Angular,因此我们决定采用基于 token 的身份验证的 REST 方法。 不幸的是,在使用该插件时,我遇到了一个迄今为止无法修复的
我有一个小型演示包,已将其发布到 Azure Devops - 我可以通过启动虚拟环境并将特定的全局设置添加到我的 pip.conf 文件。 现在,我想将其安装到我的 databricks 集群中 -
我有一个小型演示包,已将其发布到 Azure Devops - 我可以通过启动虚拟环境并将特定的全局设置添加到我的 pip.conf 文件。 现在,我想将其安装到我的 databricks 集群中 -
我是一名优秀的程序员,十分优秀!