Spring注入一个使用静态方法调用的实例创建的对象

发布于 2024-12-05 15:14:47 字数 355 浏览 3 评论 0原文

我创建了一个对象,如下所示:

 serviceValidatorObject = ServiceFactory.getInstance().getServiceValidator()

该对象是通过调用静态方法 getInstance(),然后调用实例方法 getServiceValidator() 创建的。

我想使用 Spring 注入来配置它。

问题是 ServiceFactory 是我无法更改的遗留代码。

我知道 Spring 支持通过工厂类静态或实例方法进行注入,但是我是否可以在 Spring 中配置上面的对象创建?

I have an object created as follows:

 serviceValidatorObject = ServiceFactory.getInstance().getServiceValidator()

The object is created from a call to static method, getInstance(), then an instance method, getServiceValidator().

I want to use Spring injection to configure this instead.

The trouble is the ServiceFactory is legacy code that I cannot change.

I know Spring supports injection through a factory class static or instance method, but is there anyway I can configure the object creation above in Spring?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

书信已泛黄 2024-12-12 15:14:47
<bean id="exampleBean"
      factory-bean="myFactoryBean"
      factory-method="createInstance"/>

在您的例子中,exampleBean 是您的 serviceValidatorObject,factory-bean 是 serviceFactory,工厂方法是 getServiceValidator。你可能也需要这个:

<bean id="serviceFactory"
      factory-method="getInstance"/>
<bean id="exampleBean"
      factory-bean="myFactoryBean"
      factory-method="createInstance"/>

In your case, exampleBean is your serviceValidatorObject, factory-bean is serviceFactory, and the factory-method is getServiceValidator. You might need this, too:

<bean id="serviceFactory"
      factory-method="getInstance"/>
紫竹語嫣☆ 2024-12-12 15:14:47

您可以使用这个 bean 定义:

<bean id="serviceValidatorObject" factory-bean="serviceFactory" factory-method="getServiceValidator"/>  
<bean id="serviceFactory" class="<package>.ServiceFactory" /> 

You could use this bean definition:

<bean id="serviceValidatorObject" factory-bean="serviceFactory" factory-method="getServiceValidator"/>  
<bean id="serviceFactory" class="<package>.ServiceFactory" /> 
述情 2024-12-12 15:14:47

您应该能够使用以下方法实现此目的:

<bean id="myCreatedObjectBean" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass">
        <value>com.mycompany.MyFactoryClass</value>
    </property>
    <property name="targetMethod">
        <value>myFactoryMethod</value>
    </property>
</bean>

然后您可以使用 @Resource 或 @Autowired + @Qualifier 直接注入到您的对象中。

对于此示例,您的 applicationContext.xml 应包含:

<!-- this allows Spring to create an instance of your Factory -->
<bean id="serviceFactoryBean" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass">
        <value>yourpackage.ServiceFactory</value>
    </property>
    <property name="targetMethod">
        <value>getInstance</value>
    </property>
</bean>

<!-- this allows Spring to use the factory instance to create the instance of ServiceValidator -->
<bean id="serviceValidatorBean" factory-bean="serviceFactoryBean" factory-method="getServiceValidator"/>

然后您可以通过以下方式注入到您的代码中:

@Resource(name = "serviceValidatorBean")
ServiceValidator serviceValidator;

You should be able to achieve this using:

<bean id="myCreatedObjectBean" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass">
        <value>com.mycompany.MyFactoryClass</value>
    </property>
    <property name="targetMethod">
        <value>myFactoryMethod</value>
    </property>
</bean>

Then you can use either @Resource or @Autowired + @Qualifier to inject into your object directly.

For this example, your applicationContext.xml should contain:

<!-- this allows Spring to create an instance of your Factory -->
<bean id="serviceFactoryBean" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass">
        <value>yourpackage.ServiceFactory</value>
    </property>
    <property name="targetMethod">
        <value>getInstance</value>
    </property>
</bean>

<!-- this allows Spring to use the factory instance to create the instance of ServiceValidator -->
<bean id="serviceValidatorBean" factory-bean="serviceFactoryBean" factory-method="getServiceValidator"/>

And then you could inject into your code via:

@Resource(name = "serviceValidatorBean")
ServiceValidator serviceValidator;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文