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;
}
}
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:
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.
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.
发布评论
评论(2)
来自文档
From docs
它只是意味着
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 calledgetPreviousDate
. Your factory creating the objects iscom.test.checkDate
. We don't know which type is the object returned, is not said in the configuration, probably isjava.util.Date
.The
constructor-arg
are simply passed as parameters togetPreviousDate
. Since the method is static, it doesn't need an instance ofcheckDate
. If it sounds funny to useconstructor-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.