获取来自Groovy脚本的Spring @Value

huangapple go评论67阅读模式
英文:

Get Spring @Value from groovy script

问题

在Java代码中:

@Value("${myVar}") private String myVar;

在Groovy代码中:

import org.springframework.beans.factory.annotation.Value;
import groovy.transform.Field;
@Field @Value('${myVar}') String myVar;
return myVar;

返回结果为 null。

在不传递BeansmyVar的情况下,如何从Groovy脚本中获取myVar

英文:

Inside Java code:

@Value("${myVar}") private String myVar;

Inside Groovy code:

import org.springframework.beans.factory.annotation.Value;
import groovy.transform.Field;
@Field @Value('${myVar}') String myVar;
return myVar;

returns null.

How to get myVar from Groovy script, assuming the Beans and myVar are not passed in?

答案1

得分: 1

如果我理解正确,您希望在Groovy脚本执行期间使Spring上下文可用,并对脚本属性进行常规注入。

Groovy脚本被编译为Groovy类,因此您应该可以简单地使用beanFactory.autowireBean(this)。但是,要首先获取对BeanFactory的引用,您需要手动启动一个Spring上下文,如下所示:

new ClassPathXmlApplicationContext('path/to/applicationContext.xml').withCloseable { context ->

    context.autowireCapableBeanFactory.autowireBean(this)
    ... //您的脚本的其余部分在这里
}

(您可能需要调整new ClassPathXmlApplicationContext("path/to/applicationContext.xml")部分,以适应您的需求,具体取决于您希望使用哪种方法引导Spring上下文;此外,如果脚本是较大代码库的一部分,您可能希望创建一个比应用程序的其余部分更小的应用程序上下文,以节省启动时间并避免常见的副作用。当然,这取决于用例)。

我唯一不确定的部分是在将变量转换为字段时,@Field是否保留原始注解,但如果您没有收到任何错误消息,我会认为情况是如此的。

如果注解没有保留,您始终可以声明一个简单的带注解的POJO Bean,作为可注入属性的容器,并调用def myPojo = new Pojo(); context.getBeanFactory()).autowireBean(myPojo)

英文:

If I understand correctly, you want Spring context to be available during your Groovy script execution and perform the usual injection on your script properties.

Groovy scripts are compiled to Groovy classes, and so you should be able to simply use beanFactory.autowireBean(this). However, to obtain a reference to a BeanFactory in the first place, you need to manually start a Spring context like so:

new ClassPathXmlApplicationContext('path/to/applicationContext.xml').withCloseable { context ->

    context.autowireCapableBeanFactory.autowireBean(this)
    ... //the rest of your script goes here
}

(You might need to adjust the new ClassPathXmlApplicationContext("path/to/applicationContext.xml") part to suit your needs, depending on what method you prefer to use to bootstrap your Spring context; also, if the script is part of a larger codebase, you might want to craft a smaller application context than for the rest of the app, to save startup time and avoid the usual side effects. Depends on the use case, though).

The only part I'm unsure of is whether @Field retains the original annotations when transforming a variable to a field, but if you're not getting any errors, I would assume that to be the case.

In case annotations are not retained, you can always declare a simple annotated POJO bean acting as a container for the injectable properties and call def myPojo = new Pojo(); context.getBeanFactory()).autowireBean(myPojo) instead.

答案2

得分: 0

你需要确保Groovy脚本由Spring管理,你可以在类上添加@Service或@Component注解。请参考下面的示例:

@Service
class Demo {

  public @Value('${valueA}') String valueA

  String showValue() {
    return valueA
  }

}
英文:

You need to make sure the groovy script is managed by spring, you can either add the @Service or @Component annotations on the class. see example below

@Service
class Demo {

  public @Value('${valueA}') String valueA

  String showValue() {
    return valueA
  }

}

huangapple
  • 本文由 发表于 2020年9月30日 08:36:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64129395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定