春季3 @Component和静态工厂方法 [英] Spring 3 @Component and static factory method
本文介绍了春季3 @Component和静态工厂方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我在写一个静态的工厂方法来创建对象,我怎么使用@Component注释为该工厂类和指示(有一些注释)静态工厂方法,它应该被称为创造豆类类?以下是我的意思伪code:
@Component
类MyStaticFactory
{
@<有些注解>
公共静态为MyObject的getObject()
{
// code创建/返回实例
}
}
解决方案
恐怕你不能做到这一点目前。然而,它是pretty简单的Java配置:
@Configuration
公共类CONF { @豆
公众为MyObject myObject的(){
返回MyStaticFactory.getObject()
}}
在这种情况下, MyStaticFactory
不需要任何Spring注解。当然,你可以用好醇'XML来代替。
If I'm writing a static factory method to create objects, how do I use the '@Component' annotation for that factory class and indicate (with some annotation) the static factory method which should be called to create beans of that class? Following is the pseudo-code of what I mean:
@Component
class MyStaticFactory
{
@<some-annotation>
public static MyObject getObject()
{
// code to create/return the instance
}
}
解决方案
I am afraid you can't do this currently. However it is pretty simple with Java Configuration:
@Configuration
public class Conf {
@Bean
public MyObject myObject() {
return MyStaticFactory.getObject()
}
}
In this case MyStaticFactory
does not require any Spring annotations. And of course you can use good ol' XML instead.
这篇关于春季3 @Component和静态工厂方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文