Java Spring Wire 是一个静态类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1192823/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 00:43:48  来源:igfitidea点击:

Spring Wire a Static Class

javaspring

提问by digiarnie

I'm dealing with a legacy code base where a class which is not wired up in spring needs to obtain a class that iswired up in spring. I was hoping to create a factory class that was wired up on startup and then I could just call the getInstance() method to obtain a wired up object. What is the best way to go about this?

我处理遗留代码库,其中未在春天有线了一类需要获得一类在春季接线。我希望创建一个在启动时连接的工厂类,然后我可以调用 getInstance() 方法来获取连接的对象。解决这个问题的最佳方法是什么?

Example:

例子:

public class LegacyA {
    public void doSomething() {
        ...
        Foo foo = FooFactory.getInstance();
        ...
    }
}

public class FooFactory {
    private static Foo foo;

    public static Foo getInstance() {
        if (foo == null) throw new IllegalStateException();
        return foo;
    }
}

I need FooFactory to be wired up on startup so that LegacyA can simply call getInstance() so that it returns an instance of Foo (which is also a bean defined in the application context).

我需要在启动时连接 FooFactory,以便 LegacyA 可以简单地调用 getInstance() 以便它返回 Foo 的实例(它也是应用程序上下文中定义的 bean)。

<bean id="legacyA" class="LegacyA"/>

<bean id="foo" class="Foo"/>

<!-- I need this bean to be injected with foo so that the FooFactory can return a foo -->
<bean id="fooFactory" class="FooFactory"/>

Edit: I had to re-work my example a bit as I got it a bit confuzzled in my own head...

编辑:我不得不重新编写我的例子,因为我在自己的脑海中有点困惑......

采纳答案by skaffman

Using statics like this really goes against the grain of Spring IoC, but if you really haveto use them, then I would suggest writing a simple Spring hook which takes the Fooand injects it into the FooFactory, e.g.

使用像这样的静态确实违背了 Spring IoC 的原则,但是如果你真的必须使用它们,那么我建议编写一个简单的 Spring 钩子,它接受Foo并将它注入到FooFactory,例如

public class FooFactoryProcessor implements InitializingBean {

    private Foo foo;

    public void setFoo(Foo foo) {
        this.foo = foo;
    }

    public void afterPropertiesSet() throws Exception {
        Foofactory.setFoo(foo);
    }
}

And in your XML:

在你的 XML 中:

<bean id="foo" class="Foo"/>

<bean class="FooFactoryProcessor">
   <property name="foo" ref="foo"/>
</bean>

No need to modify Fooor FooFactory

无需修改FooFooFactory

回答by Brian Agnew

Is defining the bean as a singletonin the Spring configuration of use here ? You can then inject it into LegacyBusing property or constructor inject (my preference is the latter) and then only the one instance is available.

是否在此处使用的 Spring 配置中将 bean 定义为单例?然后,您可以将其注入LegacyBusing property 或构造函数注入(我的偏好是后者),然后只有一个实例可用。

EDIT: Re. your changed question (!) I not sure why you don't simply inject Foo again as a singleton into your factory. Note also that you can use the getInstance()method via the Spring configs by using factory-method, and maintain injection through all classes.

编辑:重新。你改变的问题(!)我不知道你为什么不简单地再次将 Foo 作为单身人士注入你的工厂。另请注意,您可以使用factory-methodgetInstance()通过 Spring 配置使用该方法,并通过所有类保持注入。

回答by Gregory Mostizky

In addition to skaffman's answer you must be very very careful about initialization order.

除了 skaffman 的回答之外,您还必须非常小心初始化顺序。

When using Spring beans only the framework will automatically figure out the correct order of initializing stuff. As soon as you are doing singleton tricks however, this may break if you are not careful.

当使用 Spring bean 时,只有框架会自动找出初始化内容的正确顺序。然而,一旦你在做单例技巧,如果你不小心,这可能会中断。

In other words make sure that LegacyA cannot be run before application context finishes loading.

换句话说,确保在应用程序上下文完成加载之前不能运行 LegacyA。