Announcement Announcement Module
Collapse
No announcement yet.
Spring context creating don't fail if bean constructor threw Exception Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring context creating don't fail if bean constructor threw Exception

    Simple example.
    My classes:
    Code:
    public class A implements IA {
    
      @Inject
      public A(IB b) {
      }
    
    }
    
    public class B implements IB {
    
      @Inject
      public B(String str) throws Exception {
        throw new Exception();
      }
    }
    
    @Repository
    public class C {
    
      @PersistenceContext
      private EntityManager em;
    
    }
    
    public class StartTest {
    
      public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("testContext.xml");
        context.registerShutdownHook();
      }
    
    }
    and testContext.xml is
    HTML Code:
    <beans ...>
    
      <context:annotation-config />
    
      <bean id="jpaProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="properties">
          <props>
            <prop key="javax.persistence.validation.mode">ddl, callback</prop>
            <prop key="hibernate.connection.useUnicode">true</prop>
            <prop key="hibernate.connection.characterEncoding">UTF-8</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
            <prop key="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</prop>
    
            <prop key="hibernate.connection.username">username</prop>
            <prop key="hibernate.connection.password">password</prop>
    
            <prop key="hibernate.connection.url">jdbc:hsqldb:mem:dbname</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
    
            <prop key="hibernate.cache.use_second_level_cache">false</prop>
            <prop key="hibernate.cache.use_query_cache">false</prop>
            <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.internal.NoCachingRegionFactory</prop>
          </props>
        </property>
      </bean>
    
      <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="localDS"/>
        <property name="jpaProperties" ref="jpaProperties" />
      </bean>
    
      <bean class="C" />
    
      <bean id="bService" class="B">
        <constructor-arg type="String"><value>initString</value></constructor-arg>
      </bean>
    
      <bean id="aServiceProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="target">
          <bean class="A" />
        </property>
      </bean>
    
    </beans>
    B class always throw Exception in constructor.
    But Spring says "support.DefaultListableBeanFactory: Ignoring bean creation exception on FactoryBean type check: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'aServiceProxy'" and continuing creating context (and in if i have more than 5 proxies like "aServiceProxy" spring context creating running for few minutes before fails).

    How can i tell Spring to fail immediately if some beans threws exceptions in constructors?
Working...
X