怎样才能有工厂方法但没有工厂的Spring bean呢?

发布于 2024-12-12 12:27:38 字数 1432 浏览 4 评论 0原文

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

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

发布评论

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

评论(2

︶葆Ⅱㄣ 2024-12-19 12:27:38

来自文档

bean 定义中指定的构造函数参数将是
用于作为参数传递给 ExampleBean 的构造函数。
现在考虑一个变体,而不是使用构造函数,
Spring被告知调用静态工厂方法来返回一个实例
对象的:

<bean id="exampleBean" class="examples.ExampleBean"
      factory-method="createInstance">
  <constructor-arg ref="anotherExampleBean"/>
  <constructor-arg ref="yetAnotherBean"/>
  <constructor-arg value="1"/> 
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

public class ExampleBean {

    // a private constructor
    private ExampleBean(...) {
      ...
    }

    // a static factory method; the arguments to this method can be
    // considered the dependencies of the bean that is returned,
    // regardless of how those arguments are actually used.
    public static ExampleBean createInstance (
            AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {

        ExampleBean eb = new ExampleBean (...);
        // some other operations...
            return eb;
    }
}

请注意,静态工厂方法的参数是通过提供的
constructor-arg 元素,与构造函数完全相同
实际被使用过。此外,重要的是要认识到,
工厂方法返回的类不必是
与包含静态工厂方法的类具有相同的类型,
尽管在本例中确实如此。实例(非静态)工厂
方法将以基本相同的方式使用(除了
使用factory-bean属性而不是class属性),
所以这里不再讨论细节。

From docs

the constructor arguments specified in the bean definition will be
used to pass in as arguments to the constructor of the ExampleBean.
Now consider a variant of this where instead of using a constructor,
Spring is told to call a static factory method to return an instance
of the object:

<bean id="exampleBean" class="examples.ExampleBean"
      factory-method="createInstance">
  <constructor-arg ref="anotherExampleBean"/>
  <constructor-arg ref="yetAnotherBean"/>
  <constructor-arg value="1"/> 
</bean>

<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

public class ExampleBean {

    // a private constructor
    private ExampleBean(...) {
      ...
    }

    // a static factory method; the arguments to this method can be
    // considered the dependencies of the bean that is returned,
    // regardless of how those arguments are actually used.
    public static ExampleBean createInstance (
            AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {

        ExampleBean eb = new ExampleBean (...);
        // some other operations...
            return eb;
    }
}

Note that arguments to the static factory method are supplied via
constructor-arg elements, exactly the same as if a constructor had
actually been used. Also, it is important to realize that the type of
the class being returned by the factory method does not have to be of
the same type as the class which contains the static factory method,
although in this example it is. An instance (non-static) factory
method would be used in an essentially identical fashion (aside from
the use of the factory-bean attribute instead of the class attribute),
so details will not be discussed here.

所谓喜欢 2024-12-19 12:27:38

它只是意味着 com.test.checkDate 有一个名为 getPreviousDate 的静态方法。创建对象的工厂是 com.test.checkDate。我们不知道返回的对象是什么类型,配置中没有说,可能是java.util.Date

定义中没有指定返回对象的类型(类),只指定包含工厂方法的类。

constructor-arg 只是作为参数传递给 getPreviousDate。由于该方法是静态的,因此不需要 checkDate 的实例。如果使用constructor-arg来调用技术上不是构造函数的方法听起来很有趣,那么可以认为静态方法确实在构造一个对象,因此会更容易记住。

由于在答案的早期版本中您提到“没有工厂”,也许您正在考虑 使用实例工厂方法进行实例化,这需要factory-bean 属性,但这是使用静态工厂方法实例化

It simply means that com.test.checkDate has a static method called getPreviousDate. Your factory creating the objects is com.test.checkDate. We don't know which type is the object returned, is not said in the configuration, probably is java.util.Date.

the definition does not specify the type (class) of the returned object, only the class containing the factory method.

The constructor-arg are simply passed as parameters to getPreviousDate. Since the method is static, it doesn't need an instance of checkDate. If it sounds funny to use constructor-arg for calling a method that is not technically a constructor, think that the static method is indeed constructing an Object, so it will be easier to remember.

Since in an earlier version of your answer you mentioned "without factory", maybe you're thinking about the case of instantiation using an instance factory method, which requires the factory-bean attribute, but this is the case of Instantiation with a static factory method.

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