tags:

views:

124

answers:

6

im trying to load spring beans using XmlWebApplicationContext setConfigLocations method however i keep getting a

BeanIsAbstractException

I know that the bean is abstract, i have it configured this way, so Spring should know not to try instantiate

im 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]));

Anyone any ideas.

A: 

yep sure does

combi001
+1  A: 

The following code will try, and fail to create an instance of your abstract class:

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

Just so there is no confusion, 'abstract' beans are not the same as abstract classes.They are primarily a convenient mechanism for reducing duplicate property settings.

A child bean definition will inherit constructor argument values, property values and method overrides from the parent, with the option to add new values. If init method, destroy method, factory bean and/or factory method are specified, they will override the corresponding parent settings.

A 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>
toolkit
A: 

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 through the string array it gives the exception

combi001
A: 

You are iterating and writing the name to say, stdout? That shouldn't cause a problem. Isn't there anything in your code that tries to force spring to instantiate the bean in question?

FooLman
A: 

yep ;

map.put(beansName[mnCnt], factory.getBean(beansName[mnCnt]));
combi001
+1  A: 
map.put(beansName[mnCnt], factory.getBean(beansName[mnCnt]));

There's your problem, isn't it? By calling getBean with the name of the abstract bean, you try to instantiate it, which will generate an exception.

Nils-Petter Nilsen