Spring 创建一个类似弹簧的布线类

Spring 创建一个类似弹簧的布线类,spring,factory,Spring,Factory,我想创建一个作为提供者的bean。 我将给出它应该返回的类以及在返回之前应该设置的属性列表 所以基本上看起来是这样的: <bean id="somethingFactory" class="foo.bar.SomethingFactory"> <property name="implClass" value="foo.bar.SomehtingImpl" /> <property name="properties"> <props>

我想创建一个作为提供者的bean。 我将给出它应该返回的类以及在返回之前应该设置的属性列表

所以基本上看起来是这样的:

<bean id="somethingFactory" class="foo.bar.SomethingFactory">
  <property name="implClass" value="foo.bar.SomehtingImpl" />
  <property name="properties">
    <props>
       <prop key="prop1">prop1Value</prop>
       <prop key="prop2">prop2Value</prop>
     </props>
   </property>
</bean>

prop1Value
prop2Value
“SomethingFactory”有一个提供()方法,该方法将返回“SomethingImpl”的实例

问题是如何使用Spring来实现这一点?

,从输入参数扩展并填充属性

下面是一个示例实现:

public class ServiceFactoryBean<T> extends AbstractFactoryBean<T> {

    private Class<T> serviceType;
    private Class<? extends T> implementationClass;
    private Map<String, Object> beanProperties;


    @Override
    public void afterPropertiesSet() {
        if (serviceType == null || implementationClass == null
                || !serviceType.isAssignableFrom(implementationClass)) {
            throw new IllegalStateException();
        }
    }

    @Override
    public Class<?> getObjectType() {
        return serviceType;
    }

    public void setBeanProperties(final Map<String, Object> beanProperties) {
        this.beanProperties = beanProperties;
    }

    public void setImplementationClass(
        final Class<? extends T> implementationClass) {
        this.implementationClass = implementationClass;
    }

    public void setServiceType(final Class<T> serviceType) {
        this.serviceType = serviceType;
    }

    @Override
    protected T createInstance() throws Exception {
        final T instance = implementationClass.newInstance();
        if (beanProperties != null && !beanProperties.isEmpty()) {
            final BeanWrapper wrapper = new BeanWrapperImpl(instance);
            wrapper.setPropertyValues(beanProperties);
        }
        return instance;
    }

}
公共类ServiceFactoryBean扩展了AbstractFactoryBean{
私有类服务类型;
私有类getObjectType(){
返回服务类型;
}
公共属性(最终映射beanProperties){
this.beanProperties=beanProperties;
}
公共void setImplementationClass(

最后一堂课非常感谢你的回答——这正是我所需要的