Grails 依赖注入在服务之外?
我有一个 Grails 应用程序,需要运行一个可能会随着时间的推移而被替换的策略。我知道 Spring 是 Grails 的基础,所以我想知道我是否可以访问 Spring 的 IoC 容器,以便我可以在 xml 文件中外部化实际依赖项(注意:我从未真正这样做过,但只是知道它,所以我可能会缺少一些东西)。我的目标是能够执行如下操作:
class SchemaUpdateService {
public int calculateSomething(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
IStrategy strat = (IStrategy) ctx.getBean("mystrat");
}
}
然后在 beans.xml 文件中映射适当的实现。我认为 Grails 中支持这一点。有谁有关于这将如何工作的任何文档吗?我真的只需要 Spring IoC 库并且它就能工作吗?谢谢!
I have a Grails application that needs to run a strategy that will likely be swapped out over time. I know Spring underlies Grails, so I was wondering if I had access to Spring's IoC container so that I could externalize the actual dependency in an xml file (note: I have never actually done this, but just know of it, so I may be missing something). My goal is to be able to do something like the following:
class SchemaUpdateService {
public int calculateSomething(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
IStrategy strat = (IStrategy) ctx.getBean("mystrat");
}
}
And then map the appropriate implementation in the beans.xml file. I assume this is supported in Grails. Does anyone have any documentation on how this would work? Do I really just need the Spring IoC library and it will just work? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在
resources.xml
或resources.groovy
中定义您的 bean。 grails 文档 非常清楚如何访问Spring应用程序上下文。You define your beans in
resources.xml
orresources.groovy
. The grails documentation is very clear about how to access the Spring application context.您可以使用以下方法从任何 Grails 工件访问应用程序上下文
然后可以使用它来检索您感兴趣的任何 bean:
在无法访问
grailsApplication
的类中,您可以使用这样的帮助器如下所示来访问应用程序上下文和其中的 bean但是,只有当您需要在运行时检索同一 bean 的不同实现时,才需要这样做。如果所需的 bean 在编译时已知,只需在
resources.xml
或resources.groovy
中将这些 bean 连接在一起You can access the application context from any Grails artefact using
You can then use this to retrieve whichever beans you're interested in:
In classes that don't have access to
grailsApplication
, you could use a helper such as the following to access the application context and the beans thereinHowever, this should only be necessary if you need to retrieve different implementations of the same bean at runtime. If the required bean is known at compile-time, just wire the beans together in
resources.xml
orresources.groovy
首先,您要在 grails-app/conf/spring/resources.groovy 中定义策略:
然后,您只需
def
具有相同名称的属性到您的服务中:在任何 Grails 工件(例如服务和域类)中,Grails 都会自动为
myStrat
属性提供正确的值。但不要忘记,在单元测试中,您必须手动为其指定一个值,因为自动装配不会在单元测试中发生。在 Grails 工件之外,您可以使用如下内容:
在 Grails 2.0 中,Graeme 等人不赞成使用
*Holder
类(例如ApplicationHolder
和ConfigurationHolder
),所以我不太确定 Grails 2.0 方法是什么......First of all, you want to define your strategy in your
grails-app/conf/spring/resources.groovy
:Then, you simply
def
the a property with the same name into your service:In any Grails artefact (such as services and domain classes), Grails will automatically give the
myStrat
property the correct value. But don't forget, in a unit test you'll have to give it a value manually as the auto-wiring does not happen in unit tests.Outside of a Grails artefact, you can use something like:
In Grails 2.0, Graeme et al are deprecating the use of the
*Holder
classes (such asApplicationHolder
andConfigurationHolder
), so I'm not quite sure what the Grails 2.0 approach would be...