Spring BeanIsAbstractException

I am trying to load spring beans using XmlWebApplicationContext's setConfigLocations method. However, I keep getting

BeanIsAbstractException

      

I know the bean is abstract, I configured it this way, so spring needs to know that it is not trying to create it.

I am using Spring2.0.8.jar with jetspeed2.1.

Spring bean:

<bean id="ThreadPool" abstract="true" class="com.sample.ThreadPoolFactoryBean"/>

      

code:

ctx = appContext;
    appContext.refresh();
    BeanFactory factory = appContext.getBeanFactory();
    String[] beansName = appContext.getBeanFactory()
            .getBeanDefinitionNames();

...

map.put(beansName[mnCnt], factory.getBean(beansName[mnCnt]));

      

Does anyone have any idea?

+1


source to share


4 answers


map.put(beansName[mnCnt], factory.getBean(beansName[mnCnt]));

      



That's where your problem is, isn't it? By calling getBean with the name of an abstract bean, you are trying to instantiate it, which will throw an exception.

+2


source


The following code will try and fail to instantiate your abstract class:

map.put(beansName[mnCnt], factory.getBean(beansName[mnCnt]));

      

There is simply no confusion, "abstract" beans are not the same as abstract classes. They are primarily a convenient mechanism for reducing the parameters of repeated properties.

The child bean definition inherits the values ​​of the constructor argument, property values ​​and method are overridden from the parent, with the option to add new values. If init method, destroy method, factory bean and / or factory, they will override their respective parent settings.



Contrived example:

class Fruit {
    private String colour;
    private String name;
    // setters...
}

class Car {
    private String colour;
    private String manufacturer;
    // setters...
}

      

and

<!-- specifying a class for an abstract bean is optional -->
<bean id="sharedPropsBean" abstract="true">
    <property name="colour" value="red" />
</bean>

<bean id="myFruit" parent="sharedPropsBean" class="Fruit">
    <property name="name" value="apple" />
</bean>

<bean id="myCar" parent="sharedPropsBean" class="Car">
    <property name="manufacturer" value="Ferrari" />
</bean>

      

+2


source


Spring bean:

<bean id="ThreadPool" abstract="true" class="com.sample.ThreadPoolFactoryBean"/>

      

code:

ctx = appContext;
    appContext.refresh();
    BeanFactory factory = appContext.getBeanFactory();
    String[] beansName = appContext.getBeanFactory()
            .getBeanDefinitionNames();

      

Then when I iterate over the array of strings it gives an exception.

+1


source


Are you repeating and writing down the name to say stdout? This shouldn't be a problem. There is nothing in the code that is trying to force spring to instantiate the bean in the question?

0


source







All Articles