Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring does not abort bootstrapping on bean initialization Error when component scanning is enabled?

I have a web application with a spring configuration file. I have the following entry:

<bean id="flyway" class="xxx.FlywayTool" init-method="migrateOrFail"/>

The "flyway" bean is used to initialize and migrate the database.Now I have another bean defining the datasource the application should use:

<bean id="dataSource" class="..." depends-on="flyway">

this one depends on flyway to succeed.

Everything is working fine. Now, when the "flyway" bean throws an exception the bootstrapping of spring stops and the webapp startup is finished - all good.

Now i am starting to enable autowiring for certain components via:

<context:component-scan base-package="de.xxxxx.xxxxx" />

in some of the classes I depend on services which are also defined as beans in the xml configuration. and they I turn depend on the datasource mentioned above.

now the problem: as soon as I bootstrap the application now and "flyway" is throwing an exception the exception is swallowed by spring in the following section:

org.springframework.beans.factory.support.AbstractBeanFactory.getTypeForFactoryBean(String, RootBeanDefinition)

catch (BeanCreationException ex) {
   // Can only happen when getting a FactoryBean.
   if (logger.isDebugEnabled()) {
       logger.debug("Ignoring bean creation exception on FactoryBean type check: " + ex);
   }
   onSuppressedException(ex);
   return null;
}

and now spring tries, for every other dependend service (that depends on the datasource and therefore flyway) initialize all the beans which in turn results in the same procedure again and again.

This exceptional loop continues until spring is finished trying to instaniate every possible dependency instead of aborting after the first flyway error.

This strange behaviour only starts when I enable component scanning via

<context:component-scan ....

when this feature is disabled spring stops after the first flyway error happend. It also ends up in another class:

org.springframework.context.support.AbstractApplicationContext.refresh()

        catch (BeansException ex) {
            // Destroy already created singletons to avoid dangling resources.
            destroyBeans();

            // Reset 'active' flag.
            cancelRefresh(ex);

            // Propagate exception to caller.
            throw ex;
        }

so this is the behaviour i would expect in the other case too.

our spring version: 3.0.6.RELEASE

this behaviour is also present with other classes throwing any runtime execption (not just flyway) is this a bug or expected behaviour?

any help highly appreciated

marcel

like image 961
Marcel Avatar asked Jan 26 '12 16:01

Marcel


1 Answers

Put <context:component-scan... after your beans declaration in your XML file as nico_ekito said in comments.

Confirmed to work:

Marcel: wow, that seems to work. you reckon i should open a bug? or is this intended behaviour?

like image 102
MariuszS Avatar answered Nov 17 '22 06:11

MariuszS