Initializing Spring bean from static method from another Class?

旧城冷巷雨未停 提交于 2019-12-02 17:25:31

The factory-method should only contain the method name, not including the class name.

If you want to use a static factory, give the class of the factory(!) to the bean declaration, if you want to use an instance factory, give the factory-bean to the bean declaration, but don't give both: The class of the created bean is not given in the bean declaration.

So a full example should look like this, using a static factory for validatorFactory and an instance factory for validator:

<bean id="validatorFactory" 
    class="javax.validation.Validation" 
    factory-method="buildDefaultValidatorFactory" />

<bean id="validator" 
    factory-bean="validatorFactory"
    factory-method="getValidator" />

See details on the documentation:

http://static.springsource.org/spring/docs/2.0.x/reference/beans.html#beans-factory-class-static-factory-method

To answer you question - How would you create bean in this kind of a situation in Spring? - Do it exactly as shown here, or if you can, use a utility class like the LocalValidatorFactoryBean, which simplifies the Spring configuration.

Did you try this?

<bean id="validatorFactory" 
class="javax.validation.Validation" 
factory-method="buildDefaultValidatorFactory" />

<bean id="validator" 
class="javax.validation.Validator" 
factory-bean="validatorFactory"
factory-method="getValidator" />
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!