加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Grails:常规groovy类中的依赖注入

发布时间:2020-12-14 16:23:48 所属栏目:大数据 来源:网络整理
导读:假设我在grails-app / services下有一个BarService,在src / groovy下有这样的常规Groovy类’Foo’. class Foo { def barService} 有没有办法在运行时以编程方式将其转换为Spring bean?为了澄清,我想获得一个注入barService字段的BarService引用. def fooIns
假设我在grails-app / services下有一个BarService,在src / groovy下有这样的常规Groovy类’Foo’.

class Foo {
  def barService
}

有没有办法在运行时以编程方式将其转换为Spring bean?为了澄清,我想获得一个注入barService字段的BarService引用.

def fooInstance = new Foo()
magic-create-spring-bean-function(fooInstance)
assert fooInstance.barService

解决方法

有关如何获取applicationContext实例的信息,请参见 this answer.可以通过以下方式连接bean属性:

def ctx = ...
def foo = new Foo()
ctx.beanFactory.autowireBeanProperties(foo,ctx.beanFactory.AUTOWIRE_BY_NAME,false)

我建议使用常规的spring bean(可能是原型范围)而不是这种方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读