简体   繁体   English

Groovy Bean中的错误

[英]Error in groovy beans

I have this .groovy file 我有这个.groovy文件

@RestController
class SimpleBeanApp{

    @Autowired
    String text

    @RequestMapping("/")
    String index(){
        "You can do: ${text}!"
    }

    beans {
        text String, "-Spring Boot with Groovy beans-"
    }

}

When i run it I have 当我运行它时,我有

file...beans.groovy: 12: unexpected token: beans @ line 12, column 2. beans 文件... beans.groovy:12:意外令牌:beans @第12行,第2列。beans

which is the beans{ , I have tried adding an annotation @Bean before beans but it doesn't work. 这是beans{ ,我尝试在beans之前添加注解@Bean ,但是它不起作用。

You are trying to inject a String text with the @Autowired annotation at the beginning of your controller class. 您试图在控制器类的开头插入带有@Autowired批注的String text

Spring searches beans with type String in its dependency injection scopes. Spring在其依赖项注入范围内搜索String类型的bean。 The line that doesn't compile was supposed to provide that missing bean. 未编译的行应该提供缺少的bean。

I'm not familiar with groovy, but it has to look something like this: 我对groovy并不熟悉,但它必须看起来像这样:

@Bean
String text() {
    return "-Spring Boot with Groovy beans-"
}

That's how you create a bean of type String and name text . 这样便可以创建类型为String且名称为text的bean。 It will be found by springs dependency injection framework and linked to the @Autowired field at the top of your controller. 它会在springs依赖注入框架中找到,并链接到控制器顶部的@Autowired字段。

what mana sugests is 'Java' construct. 法力暗示的是“ Java”构造。 It will work, but is not groovy-ish. 它将起作用,但不是时髦的。 Indeed, groovy SHOULD allow you to create beans in a more 'groovy' way; 确实,groovy应该允许您以更“ groovy”的方式创建bean。 by providing a beans closure. 通过提供一个bean封闭。

I was searching to solve the same issue myself today. 我今天正在寻找解决同一问题的方法。 Turned out I had to find out by myself... So providing here my 2 cents. 原来,我必须自己找出答案...所以在这里提供我的2美分。

That 'beans' definition is actually a method belonging to a GroovyBeanDefinitionReader 该“ beans”定义实际上是属于GroovyBeanDefinitionReader的方法

So, you need to create one such class and invoke its 'beans' method providing it a closure will the beans definition, like in the example in the javadoc. 因此,您需要创建一个这样的类并调用其“ beans”方法,前提是该类将关闭bean定义,就像javadoc中的示例一样。 (I love groovy, but from time to time I feel like I need to understand what the code means in a C-programmer fashion to understand what I'm really doing... must be bound to ageing...) (我喜欢古怪的风格,但是我有时会觉得我需要理解C语言程序员以何种方式理解代码,以了解我的实际意思……必须要老化……)

Note you need to get hold of the application context. 注意,您需要掌握应用程序上下文。 One way I found (there could be better ways) is: 我发现的一种方法(可能有更好的方法)是:

class SimpleBeanApp implements ApplicationContextAware {
    ...
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        def beanConfig = new GroovyBeanDefinitionReader(applicationContext)
        beanConfig.beans {
            text String, "-Spring Boot with Groovy beans-"
        }
    }
}

Or, you could also put your beans configuration in a separate groovy script to be loaded while configuring your spring application. 或者,您也可以将bean配置放入单独的groovy脚本中,以在配置spring应用程序时加载。 Like: 喜欢:

static void main(String[] args) {
    SpringApplication.run ([DemoApplication,new ClassPathResource('/DemoConfig.groovy')] as Object[], args)
}

And put within a DemoConfig.groovy file the beans configuration: 并将bean配置放入DemoConfig.groovy文件中:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

beans {
    ...
}

Note that DemoConfig.groovy should be located in: /src/main/resources/DemoConfig.groovy 请注意,DemoConfig.groovy应该位于:/src/main/resources/DemoConfig.groovy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM